A
Andrea Crotti
Supposing I have something like this below:
class Packer
{
public:
Packer(int& x) : x(x) {}
int& x;
void packData(char *);
};
class Packet
{
public:
int x;
Packer p;
Packet(int x) : x(x), p(x) {}
};
int main() {
Packet p1(10);
cout << p1.x << " and " << p1.p.x << endl;
p1.x = 11;
cout << p1.x << " and " << p1.p.x << endl;
return 0;
}
so the Packer actually only have a subset of the attributes in Packet
and some more methods.
I thought that this actually would work, but actually I get
10 and 10
11 and 0
but why, should it not "follow" the what it points?
Or at worst keep the old value? Where does the 0 comes from now??
In general I'm not wasting any memory with this right?
class Packer
{
public:
Packer(int& x) : x(x) {}
int& x;
void packData(char *);
};
class Packet
{
public:
int x;
Packer p;
Packet(int x) : x(x), p(x) {}
};
int main() {
Packet p1(10);
cout << p1.x << " and " << p1.p.x << endl;
p1.x = 11;
cout << p1.x << " and " << p1.p.x << endl;
return 0;
}
so the Packer actually only have a subset of the attributes in Packet
and some more methods.
I thought that this actually would work, but actually I get
10 and 10
11 and 0
but why, should it not "follow" the what it points?
Or at worst keep the old value? Where does the 0 comes from now??
In general I'm not wasting any memory with this right?