This was more of a conceptual question, but I’ll provide a particular instance where I’m wondering about this. If I have a class that has several objects as properties, is it better to allocate them statically within the class, or dynamically during construction? For example, I have the following class (unecessary code emitted)
class OutlinedText
{
protected:
sf::Text BaseText;
sf::Text BackgroundText;
}
sf::Text are both objects as well. My question is, is it better to have them as declared above, and initialize them as follows
OutlinedText::OutlinedText(std::string &text, sf::Color MainColor, sf::Color OffsetColor, sf::Font &font, sf::Vector2f Pos, unsigned int BaseFontSize, unsigned int BackFontSize, float Offset)
{
BaseText = sf::Text(text, font, BaseFontSize);
BaseText.SetColor(MainColor);
BackgroundText = sf::Text(text, font, BackFontSize);
BackgroundText.SetColor(OffsetColor);
}
or, should i have them as pointers and allocate them with new as follows:
BaseText = new sf::Text(text, font, BaseFontSize);
BaseText->SetColor(MainColor);
and deallocate them with delete in the destructor? I know the stack allocates memory alot faster, but I think I’m double initializing the way I have now. SO is it a case by case thing or is one always better then the other? I’m still used to the C# way of doing things, so I was curious what the proper way to do this for C++ is. If I have a fundamental misunderstanding, please correct me.
Thanks in advance
Wherever possible, you should avoid explicit dynamic allocation. Dynamic allocation is relatively expensive and makes object lifetime management more difficult.
Note that your question is somewhat misleading: in the first case,
BaseTextandBackgroundTextaren’t necessarily allocated on the stack. If theOutlinedTextobject of which they are a member is allocated on the heap or is a static variable, then they won’t exist on the stack at all.You are: each of the member variables is initialized before the constructor body is entered, then in the constructor body, you assign to the member variables, so effectively they are getting “double initialized.”
You can (and in most cases should) use the constructor’s initialization list to initialize member variables:
This way, the member variables only get initialized once (via the initializers in the initializer list), not twice.
No: you should not have to write
deletein your C++ program: you should use a smart pointer (e.g.,auto_ptr,shared_ptr, orunique_ptr, depending on what kind of object lifetime you need) to ensure that dynamically allocated objects are automatically destroyed. If you need to store a collection of objects, you should use one of the Standard Library containers, likevector,map, orset, which also automatically clean up the objects that you store in them.Manually managing the lifetime of a dynamically allocated object is tedious and difficult to get right and should be avoided. You don’t just need to “clean up in the destructor,” you also need to correctly implement (or suppress automatic generation of) a copy constructor and a copy assignment operator.
C++ is fundamentally different from C#, especially with respect to object lifetimes and how objects come into existence, how they are copied, and when they are destroyed. Definitely make sure that you have a good introductory C++ book if you really want to learn the language.