Skip to main content
GameDev.net gamedev.net
🔒 Locked

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

Started by BlueChip Mar 3, 2003 at 11:26 AM 8 replies 250+ views
Original Post
BlueChip
BlueChip
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...
Zipster
Zipster
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.
smart_idiot
smart_idiot
class A* C::XXX::Pointer;

[edit: smilies]

[edited by - smart_idiot on March 3, 2003 12:35:25 PM]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
flangazor
flangazor
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.
civguy
civguy
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
Qw3r7yU10p!
Qw3r7yU10p!
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]
Zipster
Zipster
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
BlueChip
BlueChip
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]
civguy
civguy
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
};
BlueChip
BlueChip
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.