Reference class members

Hi all,

I have a question about references. Let's assume I have an abstract class A, and several derived classes A1,A2, etc...
I want to build another class B, that can "contain" an object of type A1/A2/A3/etc.., possibly not knowing which kind of inherited class it is, exploiting polimorphism.
A possible colution could be using a reference to an obj of class A, and this is what my textbook is suggesting (for the time being; then it follows with virtual constructors, etc).
I am not really confident with references: I verified that if I declare an object

1
2
3
4
5
class B
{
   public:
           A MyA_obj;
}


this turns out to be an error, because A is abstract; so I write


1
2
3
4
5
class B
{
   public:
           A& MyA_obj;
}


and the code runs. I just do not know what is happening when I create a "reference member" (are they called so?) in my class. Is MyA_obj a pointer to an object of class A or actually an object of class A itself?
More generally, could you please provide me with some infos about this usage of the reference operator? I only know its application in connection with pointers.
Many Thanks
Hi Araculsa,

in your first coding example, it won't compile since you can't instantiate a object of an abstract class.

In your second coding example, you are merely defining a reference, not creating a new object since references are basically pointers.

Pointers/references to abstract classes are used when they are bound to an object of a derived class, typically implemented with virtual functions that differ in their implementations with other derived classes but have the same interface as in the abstract class.

You can then use polymorphism by calling the virtual functions of objects of these derived classes through the reference/pointer to their abstract base class to which they are bound, resulting in the execution of the implementation specific to the class of the object in question.

Hope this helps.
Hi dangrr888,

many thanks for your help. I understand the aim of using reference/pointers, and your suggestions has been helpful. Is it correct that the only diff between ref and pointers lies in the fact that references cannot be reseated, and have to be instantiated?
Oh , and another small issue, sorry for forgetting...
later in the code I define a constructor for my B class as:
1
2
3
4
5
class B:
{       public:
                   A& MyA_Obj;
                   B(A& A_Input):MyA_Obj(A_Input)
}


Later on, in my main.cpp file, I create an obj My_B of class B having previously created an obj My_A1 of class A1; the correct code is

1
2

B My_B(My_A1);


My question is: since the constructor's argument is a ref to A obj (but A1 /A2/A£ are fine as well), shouldn't I write


1
2

B My_B(My_A1&);


?
Many Thanks
The reference to My_A1 is created when you enter the B constructor

Remember, references are just pointers (with extra rules) and so My_A1 is not owned by My_B

You have to be 100% sure that the life of My_A1 is longer then My_B, if My_A1 goes out of scope before My_B
then My_B will be fatally injured and your program will crash

smart pointers like boost::shared_ptr are fantastic
Topic archived. No new replies allowed.