Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Abstract Classes 1

Status
Not open for further replies.

edmund1978

Programmer
Jan 25, 2001
58
GB
I have made an abstract class using pure virtual functions, which has several other classes derived from it(which are not abstract). I need to make a class which has a method which can accept as a parameter, any of these dervived classes. How can I do this?
 
Take a look at this and tell me if it helps

-pete


class foo{
public:
virtual void speak() =0;
};

class myfoo : public foo{
public:
void speak(){ cout << &quot;myfoo speaks&quot; << endl; }
};

class yourfoo : public foo{
public:
void speak(){ cout << &quot;yourfoo speaks&quot; << endl; }
};

class fooUser{
public:
void useFoo( foo&amp; oFoo){ oFoo.speak(); }
};
 
This worked for my classes, but in my using class (fooUser) I can't declare a private variable of type foo;
ie.

public:
...
...

private:
foo&amp; fooObject;

fooUser::fooUser(foo&amp; thisFoo)
{
foo = thisFoo;
}


It says fooObject' : must be initialized in constructor base/member initializer list;
 
The compiler is correct.
All constant and reference data members of a class must be initialised in the initialiser list
i.e. they must have been assigned an initial value before they can be accessed (this includes the body of the constructor as well).

Try :
fooUser::fooUser(foo &thisFoo) : fooObject(thisFoo)
{
}

Nigel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top