Thread: type & getReference() const doesn't work.

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    11

    type & getReference() const doesn't work.

    The following code doesn't compile with gcc 2.96 for linux.
    The error I receive is :

    ../src/text.cxx: In method 'int & Base::getNumber() const':
    ../src/text.cxx:line7: could not convert 'this->Base::number' to 'int&'

    The problem vanish if the getNumber method is declared to return const:

    const int & getNumber() const;


    Is there a bug in gcc or I miss something?

    Best Regards,
    Cristian


    include <iostream>

    class Base {
    public:
    Base();
    public:
    int number;
    void setNumber(int number);
    int & getNumber()const ;
    };
    using std::cout;
    Base::Base() {
    cout << "\n\n\n\ncalling the ctrctor." << endl;
    }
    int & Base::getNumber()const {
    return number;
    }
    void Base::setNumber(int number) {
    this->number=number;
    }

    int main() {
    Base * pBase = new Base();
    pBase->setNumber(8);
    cout << "getNumber() called: " << pBase->getNumber() << endl;
    return 0;
    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    What's the meaning of the & ?

    int & getNumber()const ;

    Just remove it in the function and prototype.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    11
    Hi,

    The function tries to return a reference to the "number" variable.

    The code I have posted is just an example of the problem I have. The real code should take a reference to the variable.

    To make me clear: I want to have a const function that returns a reference and it doesn't work. Legally it must, in my opinion.


    BR,
    Cristian

    PS;
    also const int & getNumber() const work. But I wold like to know why the first option does't work.

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    int & Base::getNumber()const {
    return number;
    }

    should be

    int & Base::getNumber()const {
    return &number;
    }

    BTW, next time use code tags. It makes your code easier to read.

  5. #5
    endo_at_work
    Guest
    if you want to return a reference from a const function, it has to be a const reference. If not, then you are effectively able to modify a data member through a const member function - which is what they are used to prevent.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Your compiler is complaining probably quite rightly so....
    Why when you declare a function const (as in wont alter objects state) do you think that the compiler should allow you to return a non const reference. This would invalidate the const specification of the function.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    You cannot give a reference to something unmodifiable ( const ) to someone who might modify it.

    PHP Code:
    #include <iostream>

    using namespace std;

    class 
    Base 
    {
        public:
            
    Baseint number )
            {
                
    m_iNumber number;
            }

            
    void setNumberint number )
            {
                
    m_iNumber number;
            }

            const 
    intgetNumber() const
            {
                return 
    m_iNumber;
            }

            
    intgetNumber()
            {
                return 
    m_iNumber;
            }

        private:
            
    int m_iNumber;

    };

    int main() 
    {
        
    Base base(8);

        
    cout << base.getNumber() << endl// const version called

        
    base.getNumber() = 666// non-const version called, will not compile on const objects
        
    cout << base.getNumber() << endl// const version called

        
    cin >> base.getNumber(); // non-const obviously
        
    cout << base.getNumber() << endl// const version called

        
    return 0;
    }

    // As you have both a const and non const accessor function,
    // I would suggest dropping the set function and renaming both
    // getNumber functions to Number instead.
    //
    // In C++.NET this is called a property. 
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    11
    Hi,

    My view was that const just speaks about what the function does and const type indicates how the result behaves. Now it's ok.

    Thank you.

  9. #9
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Indeed. However, the behaviour of a const function includes throwing compiler errors if a user tries to circumwent it's meaning

    By using a const function, you should not be able to alter the state of the object. If this const function returned a direct reference to internal data, it would leave the object open to modification.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes & Collections
    By Max_Payne in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2007, 01:06 PM
  2. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  3. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM