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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:48:03+00:00 2026-05-30T05:48:03+00:00

I’ve been working at this all day so I hope I don’t forget any

  • 0

I’ve been working at this all day so I hope I don’t forget any important details, but here goes. My original goal was to have a player factory that encapsulated the logic of how to create a player.

Here’s what that looks like:

Player PlayerFactory::CreatePlayer() {

    Player constructee(id_generator++);

    // c++1x move semantics says this isn't a copy!
    return constructee;
}

Within the player, there is a composite PlayerInputComponent member variable. This PlayerInputComponent object encapsulates the logic of dealing with player input, to do this it needs a reference/pointer to the actual player itself. Easy enough, it takes the reference to the player in as it’s only parameter to it’s constructor.

When the player object is constructed, it’s initializer list passes a reference to itself to the PlayerInputComponent object. Here’s how it looks:

Player::Player(const unsigned int id) 
    : id(id), 
    input_component(*this)
{
...
}

I understand that de-referencing this within an initializer list is usually a bad idea, but I’m just using it to set a reference within the PlayerInputComponent object. Here’s the constructor:

PlayerInputComponent::PlayerInputComponent(Player &player) 
    : player_entity(player) { // set the reference, it is never used within the constructor body
}

For whatever reason, when the player factory returns the local copy of the player it has created, the references get garbled. My intent, was for the instance of the player created on the stack to be moved and assigned to the callee. This looks like the following:

auto player = player_factory.CreatePlayer();

After the code executes the player’s composite PlayerInputComponent object’s reference to it’s player is mangled. I understand that the move constructor for the player is invoked when the factory returns the local player object to the callee, but what happens the reference of the player within the PlayerInputComponent? Is there a better way to solve this problem? I like the semantic meaning of having a reference vs a pointer in this situation, although I did try it with a pointer and get the same thing.

Can anyone explain to me what is happening to the PlayerInputComponent’s reference to the player when the player object is moved out of the CreatePlayer() member function and assigned to “auto player”?

For completeness here are the object’s declarations:

class PlayerInputComponent {

private:
    Player &player_entity;
    void HandleKeyboardInput();
public:
    //PlayerInputComponent(PlayerInputComponent &&other);
    //PlayerInputComponent(const PlayerInputComponent &other);
    PlayerInputComponent(Player &player);
    void Update();
};

And here is the Player:

class Player : public Entity{
    friend class PlayerFactory;
private:
    const unsigned int id;

public:
    Player(const unsigned int id);

    // components
    PlayerInputComponent input_component;
};

I’ve reconstructed a small example below that shows the exact behavior, which compiles/demonstrates the problem. Thank you!

class B;

class A  {
public:
    A(B& bb) : b(bb) { }

private:
    B &b;
};

class B {
public:
    B() : a(*this) { othercrap = othercrap2 = 0; }
    int othercrap;
    int othercrap2;
private:
    A a;
};

class BFactory {
public:
    B CreateB() {
    B temp;
        return temp;
    };
};

B start() {
    BFactory fac;
    auto bi = fac.CreateB();

    return bi;
}

int main(int argc, char *argv[]) {
    auto i = start();
}
  • 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-30T05:48:05+00:00Added an answer on May 30, 2026 at 5:48 am

    Moving invalidates references. (A reference is made to a storage location, not an object)

    Remember that moving creates a new object (hence the name, move constructor). Ownership of the contents is transferred, but the object is not actually moved to a new location in memory. It is destroyed after the new object is constructed.

    You should define Player‘s copy and move constructors to properly bind the reference in the new object.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.