Sign in to follow this  

Derived Classes + Base Classes

This topic is 4751 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm wondering if this is possible.. Say I have class A that is my base class that uses class z...then I have Class C, D and E that are derived from that base class....but then I have class Z that uses class C, D, E...

// classa.h
#include "classZ.h" //if this line is in...i get tons of errors ..mainly caused
//by base class undefined
class A
{
public:
Z * ZClass;
};


//classC.h
#include "classA.h"
class C : public Class A
{
};


//classD.h
#include "classA.h"
class D : public Class A
{
};


//classE.h
#include "classA.h"
Class E : public Class A
{
};

//classZ.h
#include "classC.h"
#include "classD.h"
#include "ClassE.h"

class Z
{
public:
C * ClassC;
D * ClassD;
E * ClassE;
};

I can't include the class Z because it uses the derived classes of A...is there a way to kind of get around this...i'm not sure how the compiler or linker really works so I odn't know what its thinking....any help is appreciated..thanks kag1

Share this post


Link to post
Share on other sites

class A; // declaration

class B // definition
{
A* ptr;
};

class A // definition
: public B
{
};





A class declaration allows you to use the class in pointers, references, function declarations... things that doesn't actually need you to know the size of the class or how it gets constructed.

[Edited by - Fruny on December 15, 2004 1:35:24 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Polymorphic OOP
And don't write ": public class B" write ": public B"

I don't know what compilers you two are using that allow you to write class B there, but it's not standard.


I am allowed brainos when I'm tired.

Share this post


Link to post
Share on other sites
Fruny's example.


class A; // declaration

class B // definition
{
A* ptr;
};

class A // definition
: public B
{
};



Just to point out to the topic creator the first line is called a forward declaration. The class has been declared but not defined. And since class A inheritance from class B the variable 'A* ptr' must be protected in order for class A to have access to it.

Share this post


Link to post
Share on other sites

This topic is 4751 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this