Jump to content
  • Advertisement
Sign in to follow this  
Mantear

Copy constructor

This topic is 3920 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings, I really haven't done copy constructors before and am trying to figure out why I am getting a compile-time error. Bellow is a simple example and the error I get. The class:
class TestClass
{
public:
    TestClass() {}
    TestClass(const TestClass& test)
    {
        m_String = test.GetString();
    }
    ~TestClass() {}

    std::string GetString() {return m_String;}

private:
    std::string m_String;
};
The error:
error C2662: 'TestClass::GetString' : cannot convert 'this' pointer from 'const TestClass' to 'TestClass &'
        Conversion loses qualifiers
I'm using Visual Studio 2005 Express with SP1. Any ideas? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
TestClass::GetString needs to be a const function if you want to call it from a const object. Change it to
std::string GetString() const
{
return m_String;
}
EDIT: See here for more about const correctness.

Share this post


Link to post
Share on other sites
Thanks Kalidor, that makes sense.

However, how are Copy Constructors generally made, then? If I have a dozen or so private member variables, do I need to provide a const accessor method for all of those variables?

EDIT: Nevermind! I didn't realize that I had access to the protected/private members of the object passed into the constructor.

Share this post


Link to post
Share on other sites
Quote:
Original post by DeafManNoEars
Why not just?



TestClass(const TestClass& test)
{
m_String = test.m_String;
}


Or:
TestClass(const TestClass& test)
: m_String( test.m_String )
{}

Share this post


Link to post
Share on other sites
Quote:
Original post by Mantear
how are Copy Constructors generally made?


Like this:





That is, you normally only write one when you need one. If each data member (and base class) can "take care of itself", you should be fine. The default simply copies each data member.

The main reason this doesn't always work is because of pointers: the default copy constructor would copy *the pointer value*, whereas you would often prefer a pointer that *points at a new copy of the pointed-at thing*. However, in modern C++, we sidestep that issue by making use of standard library components and tools that take care of that for us - std::string being one of them (it automatically makes a copy of its underlying memory buffer where the characters are stored, as needed.*)

* There are actually a few possible ways to define "as needed", but the point is that you don't have to worry about it. You can treat std::string *as if* it were a primitive data type, not requiring any special cleanup, and you should strive to ensure that your own classes can say the same.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!