Returning a C++ reference in a const member functionasses
- by Chris Kaminski
A have a class hierarchy that looks somethign like this:
class AbstractDataType {
public:
virtual int getInfo() = 0;
};
class DataType: public AbstractDataType {
public:
virtual int getInfo() { };
}
class Accessor {
DataType data;
public:
const AbstractDataType& getData() const {
return(data);
}
}
Well, GCC 4.4 reports:
In member function ‘const AbstractDataType& Accessor::getData() const’:
error: invalid initialization of reference of type ‘const AbstractDataType&’ from expression of type ‘const DataType’
Where am I going wrong - is this a case where I MUST use a pointer?