Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!oliveb!apple!rutgers!att!westmark!mole-end!mat From: mat@mole-end.UUCP (Mark A Terribile) Newsgroups: comp.lang.c++ Subject: Re: Functions not defined in base class Summary: Programmer, spare that type! Message-ID: <167@mole-end.UUCP> Date: 6 May 89 02:34:14 GMT References: <897@servax0.ESSEX.AC.UK> Organization: mole-end--private system. admin: mole-end!newtnews Lines: 62 > I have a particular application where I am representing a group of > related classes by an abstract class. Now, I require that another > object of another class, is passed an instance of one of these > classes as a parameter in one of its methods. I would like to be > able to declare the abstract class as the type in the function > declaration, as I won't know until run time, which class of my group > it will be. Are you confused? Let me explain with some code... A little. Isn't this what derived types are all about? If you want other classes to interpret instructions originally designed for one class, you derive the other classes from it. You are limited for a little while longer to single inheritance, so you may have problems if you have something else you want for a base class. When Multiple Inheritance comes along, this restriction will be lifted. > I would like a function of class X ( X::A ) > void X::A( abstractClass* aMemberOfAbsClass ) > now, aMemberOfAbsClass could be any instance of a sub-class > of abstractClass. Eh? I think you mean InstanceOfAbsClass ... > X::A should be able to call a member function of aMemberOfAbsClass > that is not defined for abstractClass, should I > (1) Define an empty function with the same name as for the sub-class > in the base-class's declaration. Absolutely. That's what virtual functions are for. Provide yourself as many empty virtual functions as you need in the base class. This ensures that any call is legal for any derived object. > class Y { > int a; > public: > Y(int dd) { a = dd; } > virtual int get_both() { } > }; > > class Z : public Z { > int y; > public: > Z(int dd, int gg) : (dd) { y = gg; } > int get_both() { return y*Y::get(); } > }; I don't see a get() in the declaration of Y() . If we assume it's there, you only need the qualifier Y:: if class Z declares a get() of its own. > (2) should I do some sort of casting operation? I hope you're not seriously thinking of this! There's a mechanism provided that doesn't violate the type structure. Because of the way that pointers to virtual functions are implemented, a really well botched miscast could do all sorts of bad things. -- (This man's opinions are his own.) From mole-end Mark Terribile