2

I had a homework assignment where I had to create an abstract class named Player (code below) and a dozen derived player position classes and store an instance of each in a single vector. The code is to get the player name, player id, and player position from the user to then instantiate an object from the appropriate class, like so:

Quarterback player(name, id)

I ran into an issue when I decided to create an instance of my object by directly assigning it to a pointer variable:

Player *player = &Quarterback("John Doe", 77);

The problem when I do this is that the name attribute is empty in the new object instance but the id attribute remains intact with what I assigned it. My theory is that string is considered out of scope when I create the object this way because the object is not referenced directly in the main program. If so, why does the rest of the object still exist as normal? What is going on behind the scenes of this declaration?

Header file:

// Player.h

class Player
{
public:
    Player(std::string name, int id);

    std::string getName() const;
    int getPlayerID() const;

    virtual std::string getPlayerPosition() const = 0;
    virtual std::string play() const = 0;
    virtual std::string toString() const = 0;

private:
    std::string playerName;
    unsigned int playerID;
};


class Quarterback : public Player
{
public:
    Quarterback(std::string name, int id);
    std::string getPlayerPosition() const override;
    std::string play() const override;
    std::string toString() const override;
};

// ...

Source file:

// Player.cpp

Player::Player(string name, int id)
{
    if (name == "" || only_whitespace(name))
        throw "Name cannot be empty.";
    if (id < 1)
        throw "ID number must be greater than 0.";
    playerName = name;
    playerID = id;
}

// ...

Quarterback::Quarterback(string name, int id)
    :Player::Player(name, id)
{}

// ...

Thank you for your help!

EDIT: Here's a fuller version of my source code extracted from my homework assignment and put in a single file at this link. It doesn't compile on GCC (which is appropriate behavior according to the comments below), but it compiles with MSVC.

https://gcc.godbolt.org/z/r6vhDV

apokaliptis
  • 424
  • 1
  • 4
  • 12
  • 1
    "*The problem when I do this is that the name attribute is empty in the new object instance but the id attribute remains intact with what I assigned it.*" I think the bigger problem is the compile error you should have gotten because you can't take the address of a prvalue. – Nicol Bolas Nov 21 '19 at 05:41
  • Not sure why my question was labeled duplicate. The question I asked is not all the same as the question I'm supposedly "duplicating." I know what a dangling pointer is, and that's a whole separate question. I just didn't realize the code above created a dangling pointer. – apokaliptis Nov 21 '19 at 15:53
  • "*I just didn't realize the code above created a dangling pointer.*" If you have a problem with your car, and someone links you to an article about how to change your oil filter, then it's reasonable to assume that they're also saying that your problem is with your oil filter, which should be changed. It doesn't really help anyone to have individual answers to every possible way of producing a dangling pointer, when they all amount to "this is a dangling pointer". – Nicol Bolas Nov 21 '19 at 16:00
  • Also, your code doesn't compile. So the question should be closed until you produce compiling code. – Nicol Bolas Nov 21 '19 at 16:02
  • The code actually does compile in Visual C++ 2019 without warning. – apokaliptis Nov 21 '19 at 16:14
  • I feel like it's more comparable to if I asked what are the possible reasons my car's tie rods are going bad and somebody just sends me a video that shows how to tell if my tie rods are bad. That doesn't help me avoid ruining my tie rods. In the same way, I know there's something wrong with my code. I know what a dangling pointer is. But the link I'm being sent to doesn't tell me why my code is creating a dangling pointer. Is there a question on Stack Overflow that addresses what happens in memory when an object is being instantiated? That would actually be helpful and relevant. – apokaliptis Nov 21 '19 at 16:43
  • Your code does not conform to the C++ language, even if a particular compiler might let it happen. – Nicol Bolas Nov 21 '19 at 18:14

1 Answers1

2

Your pointer dangles:

Player *player = &Quarterback("John Doe", 77);

You are storing the address of a temporary that dies immediately.

Oblivion
  • 7,176
  • 2
  • 14
  • 33