Sign in to follow this  

c++ polymorphism with references?

This topic is 2956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?)

Share this post


Link to post
Share on other sites
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 *)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 2956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this