Accessing an inherited class's variables

Member
Posts: 53
Joined: 2006.10
Post: #16
to akb825:

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 Smile
Quote this message in a reply
Member
Posts: 53
Joined: 2006.10
Post: #17
oops, add a

delete duh; // before the return 0;
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #18
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.
Quote this message in a reply
Member
Posts: 53
Joined: 2006.10
Post: #19
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. Bored
Quote this message in a reply
Member
Posts: 53
Joined: 2006.10
Post: #20
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;
}
Quote this message in a reply
Member
Posts: 20
Joined: 2007.02
Post: #21
Thanks and oops!
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();
          }
      }
  }
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
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #22
mac_girl Wrote:
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;
}
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. Rasp

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 this message in a reply
Member
Posts: 53
Joined: 2006.10
Post: #23
Quote:you're setting p2 and duh to NULL before you delete them: that's a memory leak.

I knew that Blush
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  SQLite3 question: accessing by column instead of index? Toontingy 3 4,563 Apr 8, 2010 06:19 PM
Last Post: OneSadCookie
  C: Global Variables versus Parameters Lizard Man 10 6,424 Jan 13, 2010 08:22 PM
Last Post: Lizard Man
  Noob: Accessing Structures from Cocoa Classes MikeC 15 7,384 Oct 19, 2007 02:42 PM
Last Post: MikeC
  Problems with variables in Obj-C vnvrymdreglage 16 6,969 Oct 2, 2006 10:19 PM
Last Post: vnvrymdreglage
  Should global variables be pointers or full objects? ia3n_g 1 2,485 Aug 4, 2006 05:53 PM
Last Post: OneSadCookie