Sign in to follow this  
AAAP

Quick question (basic class/pointers stuff)

Recommended Posts

Hello y'all, i just wanna know, if i have say:
class blah
{
   int g;
   public:
          void modg(int a){ g = a; }
          int  getg(){ return g; }
};

class foo
{
   
   int h;
public:
       blah *Goo;
     foo(){
           
             Goo = new blah;
             Goo->modg(h);  
          };
     void modh(int a){ h = a; }
     int  geth(){ return h; }
};

int main(int argc, char *argv[])
{
    foo cl;
    cl.modh(15);
    cl.Goo->modg(cl.geth());
    
    cout << cl.Goo->getg() << endl;
    
        cl.modh(4);
    cl.Goo->modg(cl.geth());
    
    cout << cl.Goo->getg() << endl;
        cl.modh(10);
    cl.Goo->modg(cl.geth());
    
    cout << cl.Goo->getg() << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

just as an example, is cl.Goo->modg(int) changing the real instance of cl.Goo class, or just modifying a copy of it. I think from the test i did on this, it modifies the real thing. This applies to my game engine, where before passing control of the program to the window, it passes a pointer to the game engine itself so i can access/change game data from within the window. I think this will work but I don't want any surprises x.X

Share this post


Link to post
Share on other sites
That is changing the internal state of the blah that the Goo member points at, but continuing to point at the same blah.

Share this post


Link to post
Share on other sites
so it wouldn't be a copy, and it will affect the state of the program.

thats what I want o_O.

now.. im realizing that to do this, the Window class probably shouldn't be a member of the game class 0_0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this