Accessing an inherited class's variables
to akb825:
see the bottom part with the // comment
Code:
#include <iostream>
using namespace std;
class Parent
{
public:
Parent() { p = 1; }
void SetP(int x) { p = x; }
int GetP() const { return p; }
protected:
int p;
};
class Child: public Parent
{
public:
Child() : c(2) { SetP(c); }
int c;
};
int main (int argc, char * argv[])
{
Child* p2 = new Child();
Parent *duh = new Parent();
cout << p2->c << endl;
cout << p2->GetP() << endl;
p2->c = 5;
p2->SetP(15);
cout << p2->c << endl;
cout << p2->GetP() << endl;
// the child is a Parent...with its extra int c member
// you don't have to typecast...
duh = p2;
cout << duh->GetP() << endl;
delete p2;
return 0;
}see the bottom part with the // comment
oops, add a
delete duh; // before the return 0;
delete duh; // before the return 0;
I think you still missed the point. The point was to have a Parent that was really a Child, and accessing the member variable c. It's been pointed out that you shouldn't have that in your design, but that's beside the point.
yeah, that is weird...
I was mainly just replying to the last code the Tobs_ person said didn't work and I am bored so I made it work.
I was mainly just replying to the last code the Tobs_ person said didn't work and I am bored so I made it work.
Code:
#include <iostream>
using namespace std;
class Parent
{
public:
Parent() { p = 1; }
void SetP(int x) { p = x; }
int GetP() const { return p; }
protected:
int p;
};
class Child: public Parent
{
public:
Child() : c(2) { SetP(c); }
int c;
};
int main (int argc, char * argv[])
{
Child* p2 = new Child();
Parent *duh = new Parent();
cout << p2->c << endl;
cout << p2->GetP() << endl;
p2->c = 5;
p2->SetP(15);
cout << p2->c << endl;
cout << p2->GetP() << endl;
// the child is a Parent...with its extra int c member
duh = p2;
cout << duh->GetP() << endl;
// read the child's int into the parent's int
duh->SetP(p2->c);
cout << duh->GetP() << endl;
p2 = NULL;
duh = NULL;
delete p2;
delete duh;
return 0;
}
Thanks and oops!
So doing an object system like this is the wrong way to go about things?
That worked pretty well; then I can just make a child of cParentClass which is handled automatically.
So can someone point me in the right direction? Or did I go wrong someplace else?
Tobs
So doing an object system like this is the wrong way to go about things?
Code:
#define OMSMAXOBJ 99999
//Parent class
class cParentClass
{
public:
virtual void cycle(){};
//This is the parent class
};
void omsInit();
int omsInstanceCreate(cParentClass* obj);
void omsInstanceDestroy(int killInstance);
void omsInstanceExecute(); //Calls all cycle functions (call every frame)
//OMS list
cParentClass *omsList[OMSMAXOBJ];
void omsInit()
{
for(int ii=0; ii<OMSMAXOBJ; ii++)
{
omsList[ii]=NULL;
}
}
int omsInstanceCreate(cParentClass* obj)
{
for(int ii=0; ii<OMSMAXOBJ; ii++)
{
if(omsList[ii]==NULL)
{
omsList[ii]=obj;
return ii;
}
}
return 0;
}
void omsInstanceDestroy(int killInstance)
{
if (omsList[killInstance]==NULL)
{
printf("Error deleteing %i",killInstance);
}
else
{
delete omsList[killInstance];
omsList[killInstance]=NULL;
}
}
void omsInstanceExecute()
{
//Todo: sort by depth
for(int ii=0; ii<OMSMAXOBJ; ii++)
{
if (omsList[ii]!=NULL)
{
omsList[ii]->cycle();
}
}
}So can someone point me in the right direction? Or did I go wrong someplace else?
Tobs
mac_girl Wrote:I'm pretty sure the change to the typecasting is still the solution that he was looking for. Regardless, you're setting p2 and duh to NULL before you delete them: that's a memory leak.Code:
#include <iostream>
using namespace std;
class Parent
{
public:
Parent() { p = 1; }
void SetP(int x) { p = x; }
int GetP() const { return p; }
protected:
int p;
};
class Child: public Parent
{
public:
Child() : c(2) { SetP(c); }
int c;
};
int main (int argc, char * argv[])
{
Child* p2 = new Child();
Parent *duh = new Parent();
cout << p2->c << endl;
cout << p2->GetP() << endl;
p2->c = 5;
p2->SetP(15);
cout << p2->c << endl;
cout << p2->GetP() << endl;
// the child is a Parent...with its extra int c member
duh = p2;
cout << duh->GetP() << endl;
// read the child's int into the parent's int
duh->SetP(p2->c);
cout << duh->GetP() << endl;
p2 = NULL;
duh = NULL;
delete p2;
delete duh;
return 0;
}

Tobs_: what do you want us to point you in the right direction for? For accessing variables? You should do as much with virtual functions as possible, and limit the class-specific stuff to within those functions.
Quote:you're setting p2 and duh to NULL before you delete them: that's a memory leak.
I knew that
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| SQLite3 question: accessing by column instead of index? | Toontingy | 3 | 3,244 |
Apr 8, 2010 06:19 PM Last Post: OneSadCookie |
|
| C: Global Variables versus Parameters | Lizard Man | 10 | 4,964 |
Jan 13, 2010 08:22 PM Last Post: Lizard Man |
|
| Noob: Accessing Structures from Cocoa Classes | MikeC | 15 | 6,574 |
Oct 19, 2007 02:42 PM Last Post: MikeC |
|
| Problems with variables in Obj-C | vnvrymdreglage | 16 | 5,918 |
Oct 2, 2006 10:19 PM Last Post: vnvrymdreglage |
|
| Should global variables be pointers or full objects? | ia3n_g | 1 | 2,066 |
Aug 4, 2006 05:53 PM Last Post: OneSadCookie |
|

