c++ polymorphism with references?

Started by
7 comments, last by SiCrane 14 years, 5 months ago
Is it possible to use polymorphism with references? I had a big problem with circular dependencies relating to my EntityManager class, since a lot of other classes require callbacks to it. The solution I came up with is to pass pointers to an interface instead

#pragma once
class EntityManagerInterface
{
    public:
    virtual entity_sptr AddEntity(Entity* newentityptr)=0;
    virtual void AddEntity(entity_sptr newentity)=0;
};

typedef EntityManagerInterface* entitymanager_ptr;

My problem is that one class takes a reference to an EntityManager instead of a pointer, so I can't use this trick. Is there anything I can do? (Besides rewriting the class to use pointers?)
I trust exceptions about as far as I can throw them.
Advertisement
Yes, it is possible to use polymorphism with interfaces references.

[EDIT: OMG, 11% of my post was wrong]

[Edited by - ToohrVyk on November 7, 2009 7:53:25 AM]
I think ToohrVyk meant "references" there, unless he has some other devious way of answering your question planned :) EDIT: Apparently not!

This works fine:
class EntityManagerInterface {public:    virtual entity_sptr AddEntity(Entity* newentityptr) = 0;    virtual void AddEntity(entity_sptr newentity) = 0;};class EntityManager : public EntityManagerInterface {    // virtual functions are implemented in this class..};Entity e;EntityManager em;EntityManagerInterface& emi = em;emi.AddEntity(&e); // calls EntityManager::AddEntity(Entity *)
You seem to know what references are and what polymorphism is; would it have killed you to write a test application to see if it would work? For example:
#include <iostream>struct Base {  virtual void foo(void) const {    std::cout << "I'm a Base!\n";  }  virtual ~Base() {}};struct A : Base {  virtual void foo(void) const {    std::cout << "I'm an A!\n";  }};struct B : Base {  virtual void foo(void) const {    std::cout << "I'm a B!\n";  }};void foo(const Base & base) {  base.foo();}int main(int, char **) {  A a;  B b;  foo(a);  foo(b);  return 0;}

This took me under three minutes to write. Time from your post to the first response: 44 minutes 5 seconds.
Quote:would it have killed you to write a test application to see if it would work

Yet having code on one implementation which compiles and runs as wanted does not mean it is valid. For all the OP is concerned there could have been a language extension at play.
"You insulted me!" I did not say that in the private message Tom Sloper!
And he then could have asked the less lazy question, "This seems to do what I want, but is it guaranteed to do so?" or something similar. Not being sure of language behavior is no excuse not to do the bare minimum of investigative work on your own.
Quote:Original post by SiCrane
And he then could have asked the less lazy question, "This seems to do what I want, but is it guaranteed to do so?" or something similar. Not being sure of language behavior is no excuse not to do the bare minimum of investigative work on your own.

Please do not take my previous comment the wrong way, I fully agree with what you are saying. Any C++ language book would contain this information.
"You insulted me!" I did not say that in the private message Tom Sloper!
It only takes 3 minutes if you already know what code to try. I never realized you could use references like that.
I trust exceptions about as far as I can throw them.
Even if it takes you an order of magnitude longer with trial and error to come up with the code than it took me, that's still only 30 minutes compared to the 44 it took to get a response on the forum. And you know what references are, or you couldn't have asked the question. And you know what polymorphism is and how it works with pointers or you couldn't have asked the question. Try writing a simple example of how polymorphism works with pointers and then substitute references for pointers. Now compare how that looks with the code I posted.

Now for another trick. Put "C++ polymorphism with references", which is your thread title, into your favorite search engine. Take a look at the first ten results. If using google, then probably one of those responses will actually be this thread because google is thorough like that. However, most of those others we can assume existed before you posted this thread. This is the top result for me: Introduction to Polymorphism. If you read carefully you'll see, and this is the really amazing part, examples of using virtual functions with references.

Here's another way you could have found this information: the book "Thinking in C++" by Bruce Eckel is available as a free download from the author's website. Chapter 15 covers polymorphism and virtual functions. It also shows examples of using virtual functions with references.

This topic is closed to new replies.

Advertisement