Jump to content
  • Advertisement
Sign in to follow this  
Endar

Copy constructors and virual functions

This topic is 4851 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

class Element
{
     virtual void render(){ }
};

class Control : public Element
{
     virtual void render(){ }
};

class Button : public Control
{
     void render(){ }
};


This is my class structure so far, and my problem is that I have a vector of Control*, in which are stored pointers to Button objects. Now, everytime I call 'render' on the Control* objects, it calls the render function in the Control class, which, because it wasn't meant to be a stand-alone class, had nothing in the function definition, so, the Buttons weren't rendering. I have since moved from that and made the 'render' function in Element pure virtual, and have gotten rid of the 'render' function in the Control class, but, here's the thing, I have copy constructors for those classes that are called when a derived object needs to clone itself. So, if anyone was able to understand my ramblings, are there any ideas as to what I can do?

Share this post


Link to post
Share on other sites
Advertisement
I don't see anything wrong with that code (except maybe that the functions are private). The problem must be in the code that you left out. I wrote this program:
#include <iostream>
using namespace std;

class Element
{
public:
virtual void render(){ }
};
class Control : public Element
{
public:
virtual void render(){ }
};
class Button : public Control
{
public:
void render(){ cout << "Button" << endl; }
};

int main(int argc, char* argv[])
{
Element * e = new Button;
Control * c = new Button;
e->render();
c->render();
delete c;
delete e;
return 0;
}
And it correctly generated this output:
Button
Button

Share this post


Link to post
Share on other sites
if Element and Control are supposed not to stand-alone classes, it's better to make the relevant functions pure virtual, so you get any errors at compile-time as opposed to run-time.

class Control : public Element
{
virtual void render()=0; //The =0 makes it a pure virtual function
//The class is now an abstract base class (ABC)
//which cannot be instanciated directly.
};


Share this post


Link to post
Share on other sites
Quote:
Original post by Endar
*** Source Snippet Removed ***

This is my class structure so far, and my problem is that I have a vector of Control*, in which are stored pointers to Button objects. Now, everytime I call 'render' on the Control* objects, it calls the render function in the Control class, which, because it wasn't meant to be a stand-alone class, had nothing in the function definition, so, the Buttons weren't rendering.

I have since moved from that and made the 'render' function in Element pure virtual, and have gotten rid of the 'render' function in the Control class, but, here's the thing, I have copy constructors for those classes that are called when a derived object needs to clone itself.

So, if anyone was able to understand my ramblings, are there any ideas as to what I can do?
Have you read about the rule of three?
Quote:
Any class that has an explicitly defined destructor, copy constructor or assignment operator generally needs all three
So do you really need that copy constructor? if so, is that all you need?

Share this post


Link to post
Share on other sites
If you want Button::Render to be called you probably want it to be virtual too, no?

Share this post


Link to post
Share on other sites
Quote:
Original post by JY
If you want Button::Render to be called you probably want it to be virtual too, no?


Any method in a derived class that has the same signature as a virtual method in a base class is automatically virtual as well. Even the "virtual" specifier in the Control class is unnecessary.

Share this post


Link to post
Share on other sites
Quote:
Original post by mfawcett
Quote:
Original post by Kippesoep
Quote:
Original post by JY
If you want Button::Render to be called you probably want it to be virtual too, no?


Any method in a derived class that has the same signature as a virtual method in a base class is automatically virtual as well. Even the "virtual" specifier in the Control class is unnecessary.


You are mistaken. What you describe is name-hiding. Implementing what you describe:
*** Source Snippet Removed ***
will print out "A" (correctly), because print is not virtual. If print was virtual in the base class, "B" would be printed out.

No, he's not mistaken. You misread what he said.

Enigma

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!