This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
other/8745: "Const" method returning "non-const"reference doesn't work.
- From: cristian_negresco at yahoo dot com
- To: gcc-gnats at gcc dot gnu dot org
- Date: 28 Nov 2002 15:57:40 -0000
- Subject: other/8745: "Const" method returning "non-const"reference doesn't work.
- Reply-to: cristian_negresco at yahoo dot com
>Number: 8745
>Category: other
>Synopsis: "Const" method returning "non-const"reference doesn't work.
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: rejects-legal
>Submitter-Id: net
>Arrival-Date: Thu Nov 28 08:06:01 PST 2002
>Closed-Date:
>Last-Modified:
>Originator: Cristian Negresco
>Release: unknown-1.0
>Organization:
>Environment:
>Description:
Sorry, there are no preprocessed sources, the code doesn't compile.
Sounds stupid but take a look.
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;
}
Thank you for you time.
>How-To-Repeat:
Just try to compile.
>Fix:
const int & Base::getNumber() const
extra const for output:works, or
int &Base::getNumber() const
retrun by value.
>Release-Note:
>Audit-Trail:
>Unformatted: