I'm new to C++, and are confused about sth
I'm going to define a class, and when should I define a member a pointer? or when should I define a member a value? or when should I just define it as a reference?
what confused me is when I read the book TCPL 24.3.3 It said that "The pointer solution should be used when there is a need to change the pointer to the ‘‘contained’’ object during the life of the ‘‘containing’’ object. For example:
class C
{
int a;
public:
C(int i):a(i){};
};
class C2
{
C a;
C *p;
public:
C2(int i):a(i), p(new C(i)){};
~C2(){delete p;}
C ChangeValue(C c)
{
C temp = a;
a = c;
return temp;
}
C* ChangePointer(C *c)
{
C* temp = p;
p = c;
return temp;
}
};
I think there is no difference using a valus as a class member....
please help me through this:)