Is this the correct form of a copy constructor?

Discussion in 'C++' started by Dominique, Oct 22, 2003.

  1. Dominique

    Dominique Guest

    Hi

    Can anyone help here. I have defined a copy constructor:

    CString::CString (const CString &string) // copy constructor
    {
    Int16 size = string.GetLength() + 1; // this line generates error
    ....
    }

    and the GetLength member:

    Int16 CString::GetLength()
    {
    ....
    }

    The error on the marked line is:

    const CString as 'this' argument of 'Int16 CString::GetLength()' discards
    qualifiers

    I am using the GCC compiler, and I assume the 'const' is the source of the
    problem, but everywhere I look, the copy constructor seems to be defined
    correctly.

    Is this a problem in the compiler, or have I made an error?

    Thanks
    Dominique
     
    Dominique, Oct 22, 2003
    #1
    1. Advertising

  2. Dominique

    lilburne Guest

    Dominique wrote:

    >>

    > Is this a problem in the compiler, or have I made an error?
    >


    Yes you have an error! You are calling a non-const method on
    a const object 'string.GetLength()'.

    BTW: love the cutesy redefinition of short.
     
    lilburne, Oct 22, 2003
    #2
    1. Advertising

  3. Dominique wrote in news:Ynjlb.28629$:

    > Hi
    >
    > Can anyone help here. I have defined a copy constructor:
    >
    > CString::CString (const CString &string) // copy constructor
    > {
    > Int16 size = string.GetLength() + 1; // this line generates error
    > ...
    > }
    >
    > and the GetLength member:
    >
    > Int16 CString::GetLength()


    Int16 CString::GetLength() const
    > {
    > ...
    > }
    >


    change the declaration in your CString class too;

    class CString
    {
    // was Int16 CString::GetLength();
    Int16 CString::GetLength() const;
    };

    > The error on the marked line is:
    >
    > const CString as 'this' argument of 'Int16 CString::GetLength()'
    > discards qualifiers


    the qualifier here is the const applied to the argument of your
    copy-ctor.

    >
    > I am using the GCC compiler, and I assume the 'const' is the source of
    > the problem, but everywhere I look, the copy constructor seems to be
    > defined correctly.
    >
    > Is this a problem in the compiler, or have I made an error?
    >


    The compilers right this time.

    HTH

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Oct 22, 2003
    #3
  4. Dominique

    Mike Wahler Guest

    "Dominique" <NODOTd.b.g.e.n.e.r.a.l.0.1.LOSEDOTS@cogecoDOTca> wrote in
    message news:Ynjlb.28629$...
    > Hi
    >
    > Can anyone help here. I have defined a copy constructor:
    >
    > CString::CString (const CString &string) // copy constructor
    > {
    > Int16 size = string.GetLength() + 1; // this line generates error


    In this context, the object 'string' is const.

    > ...
    > }
    >
    > and the GetLength member:
    >
    > Int16 CString::GetLength()
    > {
    > ...


    In this context, the 'CString' object (*this) is not const.

    > }
    >
    > The error on the marked line is:
    >
    > const CString as 'this' argument of 'Int16 CString::GetLength()' discards
    > qualifiers


    It's essentially telling you that you're passing a const
    object ('string') to a nonconst member function, which will
    treat it (via the nonconst '*this') as a nonconst object,
    by 'discarding the const qualifier'. You have just thrown
    away the protection against inadvertent modification which
    the copy ctor argument type originally provided.

    > I am using the GCC compiler, and I assume the 'const' is the source of the
    > problem, but everywhere I look, the copy constructor seems to be defined
    > correctly.


    That's not where the problem is. :)

    > Is this a problem in the compiler, or have I made an error?


    You need to define 'GetLength()' as a const member function:

    Int16 CString::GetLength() const
    {
    /* etc */
    };

    And don't forget to change the prototype to match.

    *Any* member function which does not modify the object
    (such as such 'get data' functions) should be defined
    as const.

    When I first came to C++, I found these 'const' issues
    to be somewhat confusing and 'annoying', but quickly
    came to see them for the lifesavers they really are. :)

    HTH,
    -Mike
     
    Mike Wahler, Oct 22, 2003
    #4
  5. Dominique

    Mike Wahler Guest

    "lilburne" <> wrote in message
    news:bn4gbt$t1er1$-berlin.de...
    > Dominique wrote:
    >
    > >>

    > > Is this a problem in the compiler, or have I made an error?
    > >

    >
    > Yes you have an error! You are calling a non-const method on
    > a const object 'string.GetLength()'.
    >
    > BTW: love the cutesy redefinition of short.


    How do you know it's a typedef for short?
    It might be e.g. an 'int', or perhaps could even
    be some complex class in its own right.

    -Mike
     
    Mike Wahler, Oct 22, 2003
    #5
  6. Dominique

    lilburne Guest

    Mike Wahler wrote:

    > "lilburne" <> wrote in message
    > news:bn4gbt$t1er1$-berlin.de...
    >
    >>Dominique wrote:
    >>
    >>
    >>>Is this a problem in the compiler, or have I made an error?
    >>>

    >>
    >>Yes you have an error! You are calling a non-const method on
    >>a const object 'string.GetLength()'.
    >>
    >>BTW: love the cutesy redefinition of short.

    >
    >
    > How do you know it's a typedef for short?
    > It might be e.g. an 'int', or perhaps could even
    > be some complex class in its own right.
    >


    Damn you're right:
    http://dotnet.di.unipi.it/Content/sscli/docs/doxygen/fx/bcl/structSystem_1_1Int16.html

    Smalltalk anyone?
     
    lilburne, Oct 22, 2003
    #6
  7. Dominique

    David White Guest

    Mike Wahler <> wrote in message
    news:yBjlb.467$...
    >
    > *Any* member function which does not modify the object
    > (such as such 'get data' functions) should be defined
    > as const.


    Depending on what exactly you mean by "does not modify the object".

    DW
     
    David White, Oct 22, 2003
    #7
  8. Dominique

    Dominique Guest

    Thanks all, for the help.

    BTW, the Int16 is a 'Palm OS type', typedef'd from a 'signed short'!

    Dominique
     
    Dominique, Oct 22, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet
    Replies:
    8
    Views:
    4,902
    Tris Orendorff
    Apr 29, 2004
  2. Aire
    Replies:
    3
    Views:
    468
    Mike Wahler
    Jan 25, 2004
  3. ali
    Replies:
    4
    Views:
    579
    David Harmon
    Mar 5, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,245
  5. cinsk
    Replies:
    35
    Views:
    2,613
    James Kanze
    Oct 11, 2010
Loading...

Share This Page