Copy Ctor

I'm working through a Udemy course on C++ and just came across this. In my lesson on copy constructors, I kind of jumped the gun on what he was teaching, paused the video and started coding what I thought he was abt to talk abt. But I was wrong. He did something different. But my code still worked, still got the desired result. This is a damn good forum, so I know I'll get excellent feedback on this issue.

In my Player class, I have the getters/setters for name, health and xp, the only three variables used.

In my main.cpp, this is what I did:

1
2
3
4
5
6
void display_Player(const Player& source){
    std::cout << "Name:\t" << source.getName() << std::endl;
    std::cout << "Health:\t" << source.getHealth() << std::endl;
    std::cout << "XP:\t\t" << source.getXp() << std::endl;
    std::cout << "-----------------------------\n";
}


In my Player cpp/f files, I didnt create a copy constructor. But this worked anyway.

Now, in my Player cpp/h files, after turning the video back on, I was instructed to do this in my player.h

Player(const Player &source);

and this in my player.cpp

1
2
3
4
Player::Player(const Player &source)
    : name(source.name), health(source.health), xp(source.xp) {
    std::cout << "In copy ctor for " << source.name << std::endl;
}


And in my main.cpp, I changed the display_Player function to

1
2
3
4
5
6
void display_Player(Player p){
    std::cout << "Name:\t" << p.getName() << std::endl;
    std::cout << "Health:\t" << p.getHealth() << std::endl;
    std::cout << "XP:\t\t" << p.getXp() << std::endl;
    std::cout << "-----------------------------\n";
}


And it works the same as the one I originally did. So my question is this:
Is the way I did the copy constructor okay? Or is it necessary to create the copy ctor in the header file and implement the function in the class .cpp file?
You say that display_Player works even though you don't have a copy ctor. But display_Player receieves its parameter as a reference, so there is no copying.

Is the way I did the copy constructor okay?

You never made a copy ctor.
However, the way you did it is better than the way it was shown in the video since there's no reason to copy the object just to display it.
Why do you say Slyde never made a copy ctor?
Player::Player(const Player &source) is definitely a copy ctor.
@dutch - Thanks for the input. I have a long way to go...
@Ganado, I meant that he didn't make one for the first example of display_Player. The second one is from the video. He was asking why they both work and which was better (or at least that's how I interpreted the question).
Last edited on
I see. I didn't bother trying to find the video he was talking about.
it works the same as the one I originally did.

That can't be exactly true.

The first case passes Player by reference. No copy construction.

The second case passes Player by value. A copy is constructed with the constructor that you wrote.
That constructor does more than just a copy. It does:
std::cout << "In copy ctor for " << source.name << std::endl;

If one case prints "In copy ctor ..." and the other does not, then that is hardly "the same".


Now, a third case:
* Remove your copy constructor and then call the void display_Player(Player p)

The compiler does create a copy constructor, if you don't. For your class it would be like you had written:
1
2
3
Player::Player(const Player &source)
    : name(source.name), health(source.health), xp(source.xp)
{}


If your class would manage resources, e.g. contain pointer members, then the compiler's version is most likely wrong and you need to write your own.
Seek "Rule of Three" (/Five/Zero)
@keskiverto - Nice explanation. Sounded a lot like my instructor in the vid.

You said:
If your class would manage resources, e.g. contain pointer members, then the compiler's version is most likely wrong and you need to write your own.


I'm working on Deep Copies today. So here I go with using pointers in the copy process.
Last edited on
Topic archived. No new replies allowed.