This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

other/8745: "Const" method returning "non-const"reference doesn't work.


>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:


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]