Re: Undefined reference to...
On Nov 11, 10:22 am, Andrea Crotti <andrea.crott...@gmail.com> wrote:
"Alf P. Steinbach /Usenet" <alf.p.steinbach+use...@gmail.com> writes:
Problems with the above include
* 'getLower' attempts to return an object of abstract class.
* A local variable is declared with incomplete type 'Extended'.
What do you incomplete type?
Because I had the forward declaration maybe?
No, the type Extended was incomplete at the source-line
Extended e;
because only the forward declaration had been seen by the compiler and
not yet the complete definition.
A C++ compiler only does a very limited amount of look-ahead.
Basically, the only things you can refer to, which come later in the
source file, are members of a class while you are in the body of a
member-function of that same class.
* If those obstacles weren't in the way, 'getLower' would perform
a *slicing*, returning only the Base part of 'e', and then of t=
ype
'Base'.
It's not clear what you're attempting, but it may be that you want a
Meyers' singleton:
class Base
{
public:
virtual void printOut() = 0;
static Base& getLower();
};
class Extended: public Base
{
public:
void printOut() { cout << "hello"; }
};
Base& Base::getLower()
{
static Extended e;
// Whatever code you were intending to have here, then:
return e;
}
Note that with this scheme you always get the same object -- a si=
ngleton --
from 'getLower'.
Cheers & hth.,
- Alf
Anyway thanks but no, I don't think that's what I need, the simplest
solution is just to avoid the pure function so I don't have an abstract
class anymore and I can actually return something of that type.
You should be careful with the possibility of slicing.
This code
#include <iostream>
using namespace std;
class Base
{
public:
virtual void printOut() { cout<< "Base" << endl; }
static Base getLower();
};
class Extended : public Base
{
public:
void printOut() { cout<< "Extended" << endl; }
};
static Base Base::getLower() {
Extended e;
return e;
}
int main()
{
Base::getLower().printOut();
}
is guaranteed to always print "Base".
The Base::getLower function returns an object of type Base, not of
some derived type.
The other solution I guess is to use a pointer as Paul suggested.
Thanks
Or to use a smart pointer (like auto_ptr) as James suggested.
Bart v Ingen Schenau