I need to change the pointer of a object inside a class. In this example I would like the call from the member function of b b->getBase(); in the main prints the value 2, and not 1.
int main(int argc, char *argv[])
{
base *b = new base(1);
top *t = new top( b);
b->getBase();
return 0;
}
base.h
class base
{
public:
base(int _x);
void getBase();
private :
int x;
};
base.cpp
base::base(int _x):x(_x)
{
cout <<"base constructor:"<<x<<":";
};
void base::getBase()
{
cout <<"getBase()"<< x<< endl;
}
top.h
class top
{
public:
top(base *b);
private:
int topVar;
base * b;
};
top.cpp
top::top(base * _b):b(_b)
{
b->getBase();
b = new base(2);
b->getBase();
}
Results: base constructor:1 getBase():1 base constructor:2 getBase():2 getBase():1