Archived

This topic is now archived and is closed to further replies.

BlueChip

(C++) + (inheritance) = little problem

Recommended Posts

Hi folks... while I coding my game, I''ve meet a problem... It''s not a big problem, but I''m onlooker of to know if exist a solution that doesn''t obligate me to remake all... This is my problem... I''ve a class A that inherits from classes B and C.... Class C has a struct XXX, and this struct has a pointer to class A object... That give me some problems because a base class can''t to have an attribute of her superclass ... the files inclusions would be redundant... There are solutions? Or must I remake the logical structure? goodbye and thanks to your time...

Share this post


Link to post
Share on other sites
Why does class C need a pointer to type A? If A is derived from C, then C should be able to hold a C pointer, and it can point to derived objects. If you need to call A-class-only functions from that C pointer, then there''s some design issues.

Share this post


Link to post
Share on other sites
I suggest an attempt to re-order your type hierarchy more like a tree structure.

Circular references are /usually/ indicative of a flawed design. I have no metrics, however, to back up this assertion.

Share this post


Link to post
Share on other sites
I think you''ll need to forward declare A before C:

  
class B {};

class A; //forward declaration

class C { A* a };

class A : public B, public C {};

Was this the problem? I''m not sure

Share this post


Link to post
Share on other sites
Pointers to classes are fine. Just forward declare the class before its name is made use of

ie

  #include <string>

class Cat;
class Truck;

class Dog {
public:
Dog(const std::string& name);
bool Chase(Cat*);
bool Chase(Truck*);
private:
std::string name_;
};


[edited by - petewood on March 3, 2003 12:45:43 PM]

Share this post


Link to post
Share on other sites
Do direct pointers to derived classes contained within a base class still count as composite structure, or does it have to be a base class pointer as well? While we''re on the subject just thought I''d ask

Share this post


Link to post
Share on other sites
hi ... I'll try to explain my problem better


    
//****************

//class_b.h

//****************

class b
{........
.........
.........
}

-----------------------------------------
//****************

//class_c.h

//****************

#include "class_a.h" <--- to define newstruct

struct newstruct
{.....
.......
a* trallallero
}

class c
{........
.........
newstruct* XXX
}

-----------------------------------------
//****************

//class_a.h

//****************

#include "class_b.h"
#include "class_c.h"

class a: public b, c
{........
.........
.........
}


all right... that's all.
I'll remake type hierarchy, but before I would want to know how do you would arrange it.
I love to know other people's ideas.... it help me to improve =)

Ps: I don't want put "newstruct* XXX" in class_a.h because semantically it must to be in class_c.h


[edited by - BlueChip on March 3, 2003 2:55:40 PM]

Share this post


Link to post
Share on other sites
Hmm, I think you''re lacking header guards too.

But the problem you have now can be solved like this:


  
//****************

//class_c.h

//****************


class a; //THIS IS A FORWARD DECLARATION


struct newstruct
{.....
.......
a* trallallero
};

class c
{........
.........
newstruct* XXX
};

Share this post


Link to post
Share on other sites
ok, from the moment that I have already demonstrated my ignorance I can continue.............

I''ve used forward declaration, but another problem is born...
in class_c.cpp I must use this call:

(newstruct* XXX)->(Get a* trallallero) -> (method Class_b.h)

I can do it, because all class_b method are inherit by class_a, but to do this, I must include in bottom class_c.h the class_a header...... otherwise the compiler tell me that class A hasn''t (Get a* trallallero) method....

but if I put the header..... I recall to start point ^__^

I am becoming crazy O_o

Share this post


Link to post
Share on other sites