Home » Community » Forums » For Beginners » c++ polymorphism with references?
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 c++ polymorphism with references?
Post New Topic  Post Reply 
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?)

 User Rating: 1017   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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]

BlogFacebook

 User Rating: 1976   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

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



 User Rating: 1611   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 2061   |  Rate This User  Send Private MessageView ProfileView JournalView GD Showcase Entries Report this Post to a Moderator | Link

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.

 User Rating: 956   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 2061   |  Rate This User  Send Private MessageView ProfileView JournalView GD Showcase Entries Report this Post to a Moderator | Link

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.

 User Rating: 956   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

It only takes 3 minutes if you already know what code to try. I never realized you could use references like that.

 User Rating: 1017   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

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.

 User Rating: 2061   |  Rate This User  Send Private MessageView ProfileView JournalView GD Showcase Entries Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may not post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: