Conversion loses qualifiers

Discussion in 'C++' started by David Kevin, May 2, 2012.

  1. David Kevin

    David Kevin Guest

    Hi,
    I would be grateful if somebody could me help with following piece of
    code:

    #include "stdafx.h"
    #include <iostream>
    using namespace std;

    class MyClass {
    const void func() {}
    const void func2(const MyClass* myClass) {
    myClass->func();
    }
    };

    int main()
    {
    return 0;
    }

    My compiler (Visual C++ 2008) complains that there is an error at line
    8:
    ..\Problem.cpp(8) : error C2662: 'MyClass::func' : cannot convert
    'this' pointer from 'const MyClass' to 'MyClass &'
    Conversion loses qualifiers.

    I would understand that if func could change myClass. But it is
    declared as const. What happens?

    Thanks in advance for responses,
    Greetings.
     
    David Kevin, May 2, 2012
    #1
    1. Advertising

  2. David Kevin

    David Kevin Guest

    On 2 Maj, 11:41, wrote:
    > My compiler says:
    > g++ test.cpp
    > test.cpp: In member function ‘const void MyClass::func2(const MyClass*)’:
    > test.cpp:7: error: no matching function for call to ‘MyClass::func() const’
    > test.cpp:5: note: candidates are: const void MyClass::func() <near match>
    >
    > Suggesting that perhaps the thing to change is the
    > const void func() {}
    > to
    > void func() const {}
    >
    > That's the convention I've always seen, and although I am an extreme novice with regards to C++, experience with the complex semantics involved in parsing C function pointers suggests that it is not unlikely that C++'s grammar requires specific placement of the const qualifier to distinguish a function that returns, say, a constant int from a function which returns a mutable int and leaves the class constant.
    > This technicality would then undoubtedly carry over into functions that return a void (even though there is no such thing as the concept of a constant nothing) due do a desire for consistency.
    > That's just my guess.


    Thank you very much. It is what really works. Now I have recalled that
    I have read something similiar to that what you have just said in
    regular C++ handbook. But it's something strange that I couldn't see
    it in Bjarne Stroustrups' "C++ Programming Language" book to which I
    have referenced (it wasn't possible to use that function modifier in
    older standard?). On the other hand, I haven't reviewed that in enough
    details to be sure that it isn't there.
     
    David Kevin, May 2, 2012
    #2
    1. Advertising

  3. On May 2, 3:13 am, David Kevin <> wrote:
    > On 2 Maj, 11:41, wrote:
    >
    > > My compiler says:
    > > g++ test.cpp
    > > test.cpp: In member function ‘const void MyClass::func2(const MyClass*)’:
    > > test.cpp:7: error: no matching function for call to ‘MyClass::func() const’
    > > test.cpp:5: note: candidates are: const void MyClass::func() <near match>

    >
    > > Suggesting that perhaps the thing to change is the
    > > const void func() {}
    > > to
    > > void func() const {}


    Yes. In the context of a member function declaration, the former is a
    non-const member function that returns void (well, const void, which
    in practice is absolutely the same thing). The latter is a const
    member function that returns void. The 'const' after the argument
    list is a promise that the function does not affect the state of the
    object with which it is called. (Neglecting any use of mutable or
    const_cast keywords, C-style casting to non-const, or other peripheral
    issues.)


    > Thank you very much. It is what really works. Now I have recalled that
    > I have read something similiar to that what you have just said in
    > regular C++ handbook. But it's something strange that I couldn't see
    > it in Bjarne Stroustrups' "C++ Programming Language" book to which I
    > have referenced (it wasn't possible to use that function modifier in
    > older standard?). On the other hand, I haven't reviewed that in enough
    > details to be sure that it isn't there.


    TC++PL section 10.2.6 (in the 3rd edition) -- "Constant Member
    Functions".

    Hope this helps,
    - Kevin B. McCarty
     
    Kevin McCarty, May 2, 2012
    #3
  4. David Kevin

    David Kevin Guest

    On 2 Maj, 17:39, Kevin McCarty <> wrote:
    > TC++PL section 10.2.6 (in the 3rd edition) -- "Constant Member
    > Functions".
    >
    > Hope this helps,


    Yes, thanks.
     
    David Kevin, May 5, 2012
    #4
  5. David Kevin <> wrote:
    > Conversion loses qualifiers.


    This is compiler speak for "you are trying to use a const object as
    non-const". (There might be other situations that trigger the same
    message, but attempting to bypass constness without a cast is by far
    the most common one.)

    When you have a const pointer to an object, you can only call member
    functions that are marked as 'const'. (You can call non-const ones by
    removing the constness of the pointer with a const_cast, but that's
    usually *not* recommended, and should be done only as the utmost last
    resort, when no other solution is possible.) Hence the function you
    should call should have the 'const' qualifier (or it should have an
    overloaded version with that qualifier).
     
    Juha Nieminen, May 8, 2012
    #5
  6. David Kevin

    Rui Maciel Guest

    Juha Nieminen wrote:

    > This is compiler speak for "you are trying to use a const object as
    > non-const".



    I would rephrase that in a way that would be more helpful to the user, such
    as:

    "You are trying to call a non-const method of a const object".


    I've just filed a bug report for GCC with this suggestion. The bug report
    is available at:
    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53281


    Rui Maciel
     
    Rui Maciel, May 8, 2012
    #6
  7. Rui Maciel <> wrote:
    > Juha Nieminen wrote:
    >
    >> This is compiler speak for "you are trying to use a const object as
    >> non-const".

    >
    >
    > I would rephrase that in a way that would be more helpful to the user, such
    > as:
    >
    > "You are trying to call a non-const method of a const object".


    I think the same message is given also in other situations where a
    const object is attempted to be used as non-const (such as trying to
    assign the const pointer to a non-const one, or trying to give it to
    a function taking a non-const pointer).
     
    Juha Nieminen, May 8, 2012
    #7
    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. Dan  Bass
    Replies:
    2
    Views:
    485
    Eliyahu Goldin
    Nov 22, 2004
  2. Tron Thomas

    Qualifiers lost compiling STL code

    Tron Thomas, Aug 7, 2004, in forum: C++
    Replies:
    3
    Views:
    514
    Tron Thomas
    Aug 9, 2004
  3. danny van elsen
    Replies:
    6
    Views:
    41,235
    Pete Becker
    Apr 24, 2005
  4. DanielESFA
    Replies:
    17
    Views:
    1,247
    DanielESFA
    May 22, 2005
  5. Jason
    Replies:
    6
    Views:
    9,007
    Mike Wahler
    Oct 9, 2003
Loading...

Share This Page