Linked Questions

0 votes
2 answers
1k views

Return a statically or dynamically allocated object from a factory? [duplicate]

Basically, this: Cat CatFactory::CreateCat() { return Cat(); } or this: Cat* CatFactory::CreateCat() { return new Cat(); } I know the general advice is to avoid new when possible and tend to ...
user2175959's user avatar
416 votes
17 answers
412k views

Is the practice of returning a C++ reference variable evil?

This is a little subjective I think; I'm not sure if the opinion will be unanimous (I've seen a lot of code snippets where references are returned). According to a comment toward this question I just ...
52 votes
4 answers
68k views

How to perform a dynamic_cast with a unique_ptr?

I have a class hierarchy as follows: class BaseSession : public boost::enable_shared_from_this<BaseSession> class DerivedSessionA : public BaseSession class DerivedSessionB : public BaseSession ...
Sharath's user avatar
  • 1,805
14 votes
5 answers
12k views

Factory Pattern in C++ -- doing this correctly?

I am relatively new to "design patterns" as they are referred to in a formal sense. I've not been a professional for very long, so I'm pretty new to this. We've got a pure virtual interface base ...
user avatar
4 votes
6 answers
14k views

C++ static factory constructor

I am in the process of making a simulation and it requires the creation of multiple, rather similar models. My idea is to have a class called Model and use static factory methods to construct a model. ...
Wilco's user avatar
  • 978
7 votes
4 answers
4k views

Use String-To-Class Lookup table in C++ to instantiate classes

Looking for a way to avoid a massive IF/ELSE and use a lookup table to resolve strings to particular classes to instantiate, that all derive from a base class. Is something like this possible, and if ...
user1054922's user avatar
  • 2,175
8 votes
2 answers
10k views

C++ Factory Pattern implementation - example, questions, and concerns

I've been attempting to get a better understanding of using a Factory Pattern in C++ lately. I've consulted the following resources: How to implement the factory method pattern in C++ correctly ...
cdahms's user avatar
  • 3,840
7 votes
4 answers
5k views

C++ generic object factory by string name

I need a way to instantiate objects based on its class name passed by as a std::string. This is working right now, but need to be generalized: void* create(std::string name) { if(name == "...
yiown's user avatar
  • 153
2 votes
3 answers
2k views

C++ - Using content of an std::string to call a class instance of the same name

I'm trying to use the value stored in an std::string to call a class instance of the same name, for example: class myClass{int x;} myClass hello; std::string myString = "hello"; And then by doing ...
otah007's user avatar
  • 535
7 votes
2 answers
2k views

How to make object from text file C++?

I have to implement polymorphism in my project. I have a virtual class called "Account". Then there are 4 subclasses: USD, EUR, GBP, and CHF. I need to read the current balance from a text file like ...
Varkame's user avatar
  • 99
4 votes
1 answer
13k views

Using interface to export class from dll

IClass (My interface): #ifndef _ICLASS_H #define _ICLASS_H #include <sstream> namespace Test { class __declspec(dllexport) IClass { public: virtual ~IClass() {} ...
leon22's user avatar
  • 5,739
4 votes
3 answers
632 views

Instantiation of class by classname

I have multiple classes that share a common base class, like this: class Base {}; class DerivedA : public Base {}; class DerivedB : public Base {}; class DerivedC : public Base {}; Now, I need to ...
manabreak's user avatar
  • 5,617
5 votes
2 answers
414 views

Avoid creating multiple copies of code in memory

I'm new to developing on embedded systems and am not used to having very little program memory (16kB in this case) to play with. I would like to be able to create global variables, arrays, and ...
JustcallmeDrago's user avatar
2 votes
5 answers
1k views

Can I create class factory for a constructor with parameter?

I am using a class factory to create objects dynamically. I used this answer for its simplicity (and because I am using Qt). But now I realize I must add an argument to my constructor Item(bool ...
Thalia's user avatar
  • 14.8k
7 votes
2 answers
899 views

How to make objects in the deep easily configurable "from the top"?

Consider the following relation between classes: int main(int, char**) { | class Window { | class Layout { | class Box { /* Use argc/argv */ | Layout layout; | Box box; | int ...
Al.G.'s user avatar
  • 4,447

15 30 50 per page