boning@boning.applicon.UUCP (05/26/87)
I'm new to C++ and I've come across a problem trying to use derived classes as argument types for base class functions. Suppose I have a base class and another class derived from the base class. I must declare the base class before the derived class. Then, if I try to use the derived class as an argument to a function in the base class the compiler signals an error since the derived class hasn't been defined yet. Is there any way to use a derived class as an argument to a base class? I don't want to use a void pointer and lose type checking. The example I'm thinking of is a base class 'geometry' which has derived classes 'edges' and 'polygons'. I want to have a function 'get_edges' under 'geometry' which will find the edges belonging to a piece of geometry, ie class geometry { public: void get_edges(edges *); }; class edges: public geometry { ... }; Can I do this in C++? Thanks in advance, Peggy Boning. {allegra|decvax|harvard|yale|mit-eddie|mirror}!ima!applicon!boning, ...!ulowell!applicon!boning, or ...!raybed2!applicon!boning
jon@oddhack.caltech.edu (Jon Leech) (05/31/87)
Summary: Expires: Sender: Followup-To: Distribution: Keywords: In article <31900002@boning> boning@boning.applicon.UUCP writes: >Is there any way to use a derived class as an argument to a base class? >I don't want to use a void pointer and lose type checking. > >The example I'm thinking of is a base class 'geometry' which has >derived classes 'edges' and 'polygons'. I want to have a function >'get_edges' under 'geometry' which will find the edges belonging to >a piece of geometry, ie Easily, in the same way you would declare mutually-referential structures in C: class edges; // Let compiler know this class exists class geometry { ... void get_edges(edges &e); // I like references more than pointers }; class edges : public geometry { // Define the class ... }; What you probably want to do, however, is make get_edges() a member function of class edges taking an argument which is a geometry: class geometry { ... }; class edges : public geometry { // Define the class ... void get_edges(geometry &g); }; Remember that an edges contains all the information of a geometry, so you may not need the 'g' argument, which is part of the idea behind member functions (I can't tell from your example if this is the case). -- Jon Leech (jon@csvax.caltech.edu || ...seismo!cit-vax!jon) Caltech Computer Science Graphics Group __@/ ``There is only one spacefaring nation today. And it's not the United States, comrade!''
meister@cybvax0.UUCP (Philip W. Servita) (06/02/87)
In article <31900002@boning> boning@boning.applicon.UUCP writes: >Suppose I have a base class and another class derived from the base class. >I must declare the base class before the derived class. Then, if I try >to use the derived class as an argument to a function in the base class >the compiler signals an error since the derived class hasn't been defined >yet. > >Is there any way to use a derived class as an argument to a base class? >I don't want to use a void pointer and lose type checking. > .... do something like this: class b; class a { fcn(b* foo); <stuff> }; class b : a { <stuff> } the compiler will then shut up and take it. -phil