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 9076331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:05:15+00:00 2026-06-16T19:05:15+00:00

I have a constructor which uses an internal Builder object to instantiate a complex

  • 0

I have a constructor which uses an internal Builder object to instantiate a complex object. Five of the members in the data structure are of pointer types. However, using this pattern I am running into problems when the object is destroyed. The following is what my constructor looks like, with member initialization list:

Player::Player(const Builder& builder) 
    :m_name(builder._name)
    ,m_description(builder._description)
    ,m_primaryAttributes(builder._primaryAttributes)
    ,m_abilityAttributes(builder._abilityAttributes)
    ,m_armor(builder._armor)
    ,m_weapon(builder._weapon)
    ,m_inventory(new ComponentMap())
{}

The client code works well, as expected:

Player* player = Player::Builder()
    .name("Dylan")
    .description("Super bad-ass hero of the game")
    .primaryAttributes(createPrimaryAttributes())
    .abilityAttributes(createAbilityAttributes())
    .weapon(createWeapon())
    .armor(createArmor())
    .build();

However, if I omit one of the arguments in the message chain, and then destroy my Player object, bad stuff happens:

Player* player = Player::Builder()
    .name("Dylan")
    .description("Super bad-ass hero of the game")
    .primaryAttributes(createPrimaryAttributes())
    .abilityAttributes(createAbilityAttributes())
    .armor(createArmor())
    .build();

// ...

delete player;

// ...

// cleanMemory() gets called in Player::~Player()
void Player::cleanMemory()
{
    if(m_primaryAttributes != NULL )
        delete m_primaryAttributes;
    if(m_abilityAttributes != NULL )
        delete m_abilityAttributes;
    if(m_inventory != NULL )
        delete m_inventory;
    if(m_weapon != NULL)          // oops, bad stuff happens here
        delete m_weapon;
    if(m_armor != NULL)
        delete m_armor;
}

Clearly, this happens because the pointer for weapon didn’t get initialized to either NULL or an instance of Weapon object. The constructor also doesn’t appear to allow for a default of NULL (at least from what I can see) in the event that one Builder method is omitted from the chain. For now, the client must either give Weapon a pointer to NULL or an instance of an object.

Is there any possible way to get around this without revising this Builder constructor completely? Or, should this be refactored using another pattern, such as Factory, and just go back to a regular constructor with positional parameter list?

  • 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-06-16T19:05:16+00:00Added an answer on June 16, 2026 at 7:05 pm

    I sample code you referred isn’t very good code base, I would simply suggest below build pattern:

    class Builder
    {
       Weapon* BuildWeapon() { return new Weapon(); }
       Armor*  BuildArmor(); { return new Armor(); }    
    };
    
    class Player
    {
    public:
      Player(const Builder& builder) 
      : weapon_ptr(builder.BuildWeapon()),
        armer_ptr(builder.BuildArmor())
    
    private:
      std::shared_ptr<Weapon> weapon_ptr;
      std::shared_ptr<Armor>  armor_ptr;
    };   
    

    usage:

    Builder builder;
    std::shared_ptr<Player> player(new Player(builder));
    

    Or you could just

    class Player2
    {
    public:
      Player() {}
      void SetWeapon(Weapon* p) { weapon_ptr.reset(p); }
      void SetArmor(Armor* p) { armor_ptr.reset(p); }
    
    private:
      std::shared_ptr<Weapon> weapon_ptr;
      std::shared_ptr<Armer>  armer_ptr;
    };
    

    usage:

       Builder builder;
       std::shared_ptr<Player> player;
       player->SetWeapon(builder.BuildWeaper());
       player->SetArmor(builder.BuildArmor());
    

    As weapon_ptr, armer_ptr are smart pointers, there is no need to call delete for the dynamically allocated memory anymore, thus cleanMemory() function can be removed.

    This is just a simple sample, you could expand the Player’s interfaces to provide ability to build different element after player object is created.

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

Sidebar

Related Questions

I have a class with a static factory constructor which returns a pointer to
My program uses a NetworkOutput object which can be used to write data to
I have my own implementation of smart pointer which uses reference counting as ownership
I have an application which uses JTables to display data, and the cells are
I have problem with my code, which uses generic types. Why doesn't the compiler
I have a constructor which has a non-interface dependency: public MainWindowViewModel(IWorkItemProvider workItemProvider, WeekNavigatorViewModel weekNavigator)
I have a templated class Rect with conversion constructor which allows conversion between Rect
I have one constructor function, which acts as a superclass: Bla = function(a){this.a =
I have a class which does not have copy constructor or operator= overloaded. The
I have a MustInherit .NET class which declares a constructor with an integer parameter.

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.