You are here:

C++/Class design

Advertisement


Question
QUESTION:
Hi ,

class X1
{
....
};


class X2
{

...
};

class X3
{

public :

X1 *x1;  

X2  x2;

void test (X2 *x2, X1 &x1);
}


In X3 class

1.what is the  difference between  x1 and x2  ? when we have delcare like this?

2. what is the significance of  x1 and x2  and role it will play in lifetime ?

3. In test method  X2 is pointer object and X1 is reference both will change the object value, but  why one is delcared as pointer object and other is as reference?

Sorry if couldnt able to explain my problem clearly

thanks

ANSWER: The member X3::x1 is a pointer. This pointer points to an object of type X1 (which is outside the X3 object). It could also be 0 (NULL) in which case there is no object of type X1 at all.
X3::x2 is an object held by value - the X2 object is contained within the X3 object.

When an object of type X3 is created, no actual object of type X1 is created, X3::x1 is just a pointer. When the object of type X3 is destroyed, the pointer X3::x1 will go away, but nothing happens to the actual object pointed to. The lifetime of the X1 object is independent of the life time of the X3 object.

On the other hand, When an object of type X3 is created, a contained object of type X2 (X3::x2) is also created. When the object of type X3 is destroyed, the contained object is also destroyed. The lifetime of the contained X2 object is determined by the life time of the X3 object that contains it.

> In test method  X2 is pointer object and X1 is reference both will change the object value, but  why one is declared as pointer object and other is as reference?

The only reason I can think of is that in void X3::test( X2 *x2, X1 &x1 ), an object of type X2 (passed by pointer) may or may not exist - the pointer could be null. The function should check that the pointer is not null before trying to access the object. On the other hand, object of type X1 (passed by reference) must exist. The function can access and modify it without any checks. When calling the function, passing an object of type X2 (by pointer) is optional (one could just pass a NULL pointer instead), but an object of type X1 must be passed by reference. A pointer may not point, it could be a NULL pointer. There is nothing like a reference which does not refer.


---------- FOLLOW-UP ----------

QUESTION: In this method void test (X2 *x2, X1 &x1);  for X2 i can  allocate the memory dynamically , but for X1 we cant allocate the memory right?  correct me if iam wrong?

and can you explain this other than null or non-null value passing any other scenarios we can prefer passing of X2 pointer and X1 as reference objects.

Many thanks in advance..

Answer
> In this method void test (X2 *x2, X1 &x1);  for X2 i can  allocate the memory dynamically , but for X1 we cant allocate the memory right?

No. If x2 is null, it is just that the caller has not passed an object. Creating an object with a dynamic storage duration will not help as the caller of the function will not get access to it.

> other than null or non-null value passing any other scenarios we can prefer passing of X2 pointer and X1 as reference objects.

In this particular case, no.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.