Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are references and pointers equal with regards to polymorphism?

I always think of having to use pointers for polymorphism. Using the canonical example:

DrawEngine::render(Shape *shape)
{
    shape->draw();
    shape->visible(true);
}

And passing in pointer to various Shape derived classes. Does it work the same with references?

DrawEngine::render(Shape &shape)
{
     shape.draw();
     shape.visible(true);
}

Is it even valid to do:

engine.render(myTriangle); // myTriangle instance of class derived from Shape

If this works, are there any differences between the two cases? I tried to find information in Stroustrup, but I found nothing.

I reopened this because I wanted to explore just a tad more.

So at least one difference is dynamic_cast. For me, polymorphism includes the use of dynamic_cast.

Can I go

Rhomboid & r = dynamic_cast<Rhomboid &>(shape);

What happens if the cast fails? Is this any different?

Rhomboid * r = dynamic_cast<Rhomboid*>(&shape);
like image 954
pm100 Avatar asked Oct 01 '10 00:10

pm100


People also ask

Does polymorphism work with references?

Polymorphism is one of the fundamental principles of Object-Oriented Software. The term typically means that something that can have multiple forms. In object-oriented methodology, polymorphism enables writing programs that have late binding references.

Are references and pointers the same?

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.

Do you need pointers for polymorphism?

(*) Genericity, the type of polymorphism provided by templates, doesn't need pointers nor references.

Does polymorphism only work with pointers?

Polymorphism only works with non-value types: references and pointers. And since references can only be bound once, you cannot really use them in standard containers.


2 Answers

With regard to polymorphism, references work just like pointers.

like image 70
Alex Emelianov Avatar answered Oct 17 '22 04:10

Alex Emelianov


Regarding dynamic_cast, a failed cast produces a nullpointer with pointers, and results in a throw of a bad_cast (IIRC) exception with references.

One reason is that there's no such thing as a valid null-reference.

And possibly another reason (but it could just be an unintentionally useful emergent feature) is that sometimes one needs the exception and sometimes one needs the easy-to-check nullpointer, and no matter whether you have a reference or pointer at hand it takes at most a dereferencing or address operator to obtain the behavior you want.

Cheers & hth.,

like image 35
Cheers and hth. - Alf Avatar answered Oct 17 '22 05:10

Cheers and hth. - Alf