Copy constructor error, 'this' pointer conversion

This is a discussion on Copy constructor error, 'this' pointer conversion within the C++ Programming forums, part of the General Programming Boards category; Making a copy constructor, I am getting a problem. Code: CPost::CPost(const CPost& rhs) { rhs.GetMessageLength(); //error on this line } ...

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    Copy constructor error, 'this' pointer conversion

    Making a copy constructor, I am getting a problem.

    Code:
    CPost::CPost(const CPost& rhs)
    {
    	rhs.GetMessageLength(); //error on this line
    }
    error C2662: 'GetMessageLength' : cannot convert 'this' pointer from 'const class CPost' to 'class CPost &'
    Some discussions I found about this recommended taking away the const, but when I do that, MS VC++ 6.0 wont recognize it as a copy constructor. Is anyone aware of a solution to this problem?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    Try to make the function const aswell, like
    Code:
    class c
    {
    public:
        c(const c& rhs);
        int GetSomething() const { return something; }
    private:
        int something;
    };
    
    c::c(const c& rhs)
    {
        rhs.GetSomething();
    }
    Not sure if that is it but according to msdn this should solve your problem.

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    That worked, thanks. But what if the function I want to call CANNOT be const, ie, it modifies the object?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197

    Post

    Then you cant send the object as a const. At least this is how I think it is.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >But what if the function I want to call CANNOT be const, ie, it modifies the object?
    Look up the mutable keyword. I can't guarantee that this is what you want since I haven't seen your code, but it's likely.
    My best code is written with the delete key.

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    If the function that cannot be const is logically const - meaning it might change the value of some private member variables but the logical state of the object is unchanged - then like Prelude suggested you can use the mutable keyword on the variables that might change and still mark the function as const.

    However, if the function that cannot be const actually changes the state of the object, then you shouldn't be using it inside the copy constructor. It doesn't make sense to modify the argument passed into the constructor. That is why the convention is to use a const reference parameter.

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    then you shouldn't be using it inside the copy constructor. It doesn't make sense to modify the argument passed into the constructor.
    I never thought about it that way. Cheers .
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-11-2009, 07:52 AM
  2. Nonportable pointer conversion
    By s0uL in forum C Programming
    Replies: 3
    Last Post: 12-12-2007, 04:34 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21