Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4120832
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:16:24+00:00 2026-05-20T23:16:24+00:00

This was more of a conceptual question, but I’ll provide a particular instance where

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:16:25+00:00Added an answer on May 20, 2026 at 11:16 pm

    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, BaseText and BackgroundText aren’t necessarily allocated on the stack. If the OutlinedText object 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.

    I think I’m double initializing the way I have it now.

    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:

    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(text, font, BaseFontSize),         // This is the constructor's
          BackgroundText(text, font, BackFontSize)    // initializer list
    {
        BaseText.SetColor(MainColor);
        BackgroundText.SetColor(OffsetColor);
    }
    

    This way, the member variables only get initialized once (via the initializers in the initializer list), not twice.

    Should I have them as pointers and allocate them with new as follows and deallocate them with delete in the destructor?

    No: you should not have to write delete in your C++ program: you should use a smart pointer (e.g., auto_ptr, shared_ptr, or unique_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, like vector, map, or set, 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is more of a conceptual question than anything. I have a base class
this is more a conceptual question. Consider you have a php framework that runs
this was more a conceptual question, but I was wondering what the best C#
This is more of a design question. I have a template class, and I
EDIT : this question is very similar to this one but it's more conceptual
So this question is not so much technical but more sort of conceptual. I
This is more of a conceptual question and not about the underlying programming technique.
This is more of a conceptual integrity question for something that's been bothering me.
This is more of a conceptual question. After reading about Jquery Mobile, it got
This is more of a hypothetical question. I am wondering if anyone knows if/how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.