PDA

View Full Version : need C++ help with inheritance !!!!!!!! need ASAP



jubei_GL
05-29-2002, 09:44 AM
Ok, for some reason I have problems with class inheritance ..

#1)Im doing some OpenGL work where I will need to access data of structures of 2 different classes.. They both have 1 structure each.

My problem is that even with Inheritance, I was unable to access the Parent Structure throgh Child class, even though I declared Structure object under public.

My solution was to get the Address of the Parent Structure, and send it to Child structure, where then I would assign that address to Child structure, and this way I could link them and access its DATA ...

My problem for that is , I was successful to get the address accross but ONLY through the MAIN program .. You know, I would access Parent function (which returns address) in the Main, and get address, and assigned it to some variable, and then I would call Child class and send this Address to particular function .. And it Works.. However when I tried to call the Parent Function from the Child function I ALWAYS got 0's for the Address..

I thought that it doesn't matter from WHERE I call the Parent's function.. It should always have access to Parent's variabels, and should return the Address with NO Problem .

Why can't I get values from Parent Class public member variables, whenever I call Parent Class Function from Child Class functions.. However when I call Parent Class Functions from the Main, Everything works ...

My basic Declerations are like that :

class Parent
{
public:
int returnADD();
private:
int ADDstorage;
}

class Child : public Parent
{
public:
void assingADD()
private:
int store;
}

void Child:assingADD()
{
store=returnADD() // even tried store=Parent::returnADD();
}
etc .......

Oh by the way........ GO KINGs !!!!!!!!!!

kabir
05-29-2002, 09:55 AM
Ok, first things first: stop this address passing madness _right now_. It'll make you go blind http://www.opengl.org/discussion_boards/ubb/wink.gif

Second, the reason that your inheritance didn't work is that you declared your memeber as private. Private members aren't inherited. Change "private" to "protected" and inheritance should work the way you expect.

Hope this helps.

jubei_GL
05-29-2002, 10:23 AM
i know I know.. before i had it in public, and it as well did not work .... As for passing address, I have no choice .. Im storing my plane and object data in Structures, and my Camera info also in structure.. Both of them belong to different Classes, Camera class, and Object class ... When I try to compute collision in my Collision Class, I cannot access Data in neither Camera, nor Object structures ... The only other way for me was to pass Address for each structure and then link it in Collision Class .. The other way was to put everything to one Big class, which I don't want to ............................

Also, if I initialized my plain coordinates in Plane class Constructor, I was able to access plane structure in Collision class through [ex]collisionX=plane->distance etc...However when I initialize my planes in regular function of Plain Class, I no longer can access this. I know constructor thing works with planes, but not with camera, where camera coordiantes change all the time, thus I can't put them into constructor

[This message has been edited by jubei_GL (edited 05-29-2002).]

Old GLman
05-29-2002, 12:19 PM
Perhaps you could post toned down versions of your classes so we can see what your doing more cleanly? Make sure you show how your deriving classes and all that. Oh plz use code tags http://www.opengl.org/discussion_boards/ubb/smile.gif

Old GLman

henryj
05-29-2002, 01:11 PM
Perhaps you could ask this on a c++ mailing list instead of an OPENGL one

jubei_GL
05-29-2002, 01:49 PM
typedef struct
{
int x;
int y;
}plane;

class parent
{
public:
plane *pla;
parent();
void input(int,int);
void print();
int getAddress();
void freeMem();
unsigned int fake;
private:
int addrez;
};
#endif

parent: :parent()
{
fake=NULL;
addrez=NULL;
pla=new plane;
}

void parent::input(int a, int b)
{
pla->x=a;
pla->y=b;
}
int parent::getAddress()
{
fake=(int)pla;
return(fake);
}

--------------------------------------------

typedef struct
{
int w;
int z;
}came;

class child : public parent
{
public:
child();
void printF();
private:
int *address;
came *camera;
};
#endif


void child: :printF()
{
int temp;
temp=parent::getAddress();
//I also tried temp=getAddress();
}
// when I call getAddress from printF() function I always get 0 !!! . However when I call getAddress from MAIN program, it works


[This message has been edited by jubei_GL (edited 05-29-2002).]

Old GLman
05-29-2002, 04:23 PM
Hi, from the looks of it that should work fine. Did you by chance change the name of the child print to printF? Because if the parent and child classes had a function of the same name you would have to place the virtual key word in front of the parents print(). Ofcourse theres more to it than that, but nothing a good c++ book cant tell ya.

Old GLman

coredump
05-30-2002, 04:30 AM
EDIT: nevermind

b

[This message has been edited by coredump (edited 05-30-2002).]