Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace pointers with references in C++?

"I am sure there are tens of questions with the same title. Many of them are duplicate. Mine might be duplicate too, but I couldn't find any. So I try to make it very neat, short and simple."

I have an hierarchy like this:

class Shape {
public:
   virtual void virtualfunc()  { std::cout << "In shape\n"; }
};

class Circle: public Shape {
public:
   void virtualfunc()  { std::cout << "In Circle\n"; };
};

and when I use the classes with the help of pointer, the functions are called as I expected:

int main() {
   Shape shape_instance;
   Shape* ref_shape = &shape_instance ;
   Circle circle_instance;
   Circle* ref_circle = &circle_instance;

   ref_shape = dynamic_cast<Shape*> (ref_circle);
   ref_shape->virtualfunc();
}

Here the program calls the virtualfunc() of the derived class and the result is naturally : In Circle

Now, I want to get rid of the pointers, use references instead, and get the same result. So I make trivial modifications to main() to look like this:

int main() {
   Shape shape_instance;
   Shape& ref_shape = shape_instance;
   Circle circle_instance;
   Circle& ref_circle = circle_instance;

   ref_shape = dynamic_cast<Shape&>(ref_circle);
   ref_shape.virtualfunc();
}

But this time, the program calls the virtualfunc() of the base class and the result is : In Shape

I appreciate if you let me know which concept of the references I am missing and how to change the references in the main() to get the result I got in the pointer version.

thank you

like image 958
rahman Avatar asked Mar 14 '13 03:03

rahman


People also ask

How do you change a reference to a pointer?

Once a reference is established to a variable, you cannot change the reference to reference another variable. To get the value pointed to by a pointer, you need to use the dereferencing operator * (e.g., if pNumber is a int pointer, *pNumber returns the value pointed to by pNumber .

Can you emulate a reference with a pointer?

You can basically think of pointers and references as the same thing. They both refer to addresses. So if you pass an address (either as pointer or as reference) to a function, and the content of the address changes, then the change is still there at the end of the function.

Can I pass by reference in C?

It should be noted that C doesn't have pass by reference, it can only be emulated using pointers.

Why are references better than pointers?

References are used to refer an existing variable in another name whereas pointers are used to store address of variable. References cannot have a null value assigned but pointer can. A reference variable can be referenced by pass by value whereas a pointer can be referenced but pass by reference.


1 Answers

References cannot be reseated. Once you initialize the reference in the initialization, it becomes an alias to the referred object and cannot be distinguished from it. The latter assignment:

ref_shape = dynamic_cast<Shape&>(ref_circle);

really means:

shape_instance = dynamic_cast<Shape&>(ref_circle);

You can, on the other hand, bind a new reference to the object (and you don't need the dynamic_cast, as the conversion from reference to derived to reference to base is implicit):

Shape & another_ref = ref_circle;
another_ref.virtualfunc();          // Dispatches to Circle::virtualfunc
like image 65
David Rodríguez - dribeas Avatar answered Oct 26 '22 14:10

David Rodríguez - dribeas