0

Hey I was trying to multiple inherit a pure virtual function using MVS2010 Compiler. So I can run a draw for all renderable objects.

So here is the diagram

in ASCII

|Renderable            | |Entity             |
|virtual bool draw()=0;| | functions in here |

    is - a              is - a


                Shape

So it seems it wont let me inherit the pure virtual function? and implement the virtual function. Here is my code.

// Renderable.h
#ifndef H_RENDERABLE_
#define H_RENDERABLE_
class Renderable
{
 public:
    virtual bool Draw() = 0;
};
#endif


 //Shapes.h
 #ifndef H_SHAPES_
 #define H_SHAPES_
 #include "Renderable.h"
 #include "Entity.h"
 class Shapes : public Entity, public Renderable
 {
 public:
     Shapes();
     ~Shapes();

 };


 #endif

 //shapes.cpp
 #include "Shapes.h"


 Shapes::Shapes()
 {
 }


 Shapes::~Shapes()
 {
 }


 virtual void Shapes::Draw()
 {
 }

I have tried multiple things and it doesn't work also google searched.

3 Answers 3

1

First, you need to declare the draw function again in your Shapes class. Then make sure it has the same signature as the one declared in the Renderable class.

//Shapes.h
 #ifndef H_SHAPES_
 #define H_SHAPES_
 #include "Renderable.h"
 #include "Entity.h"
 class Shapes : public Entity, public Renderable
 {
 public:
     Shapes();
     ~Shapes();

     virtual bool Draw();

 };


 #endif

 //shapes.cpp

bool Shapes::Draw()
{
}
Sign up to request clarification or add additional context in comments.

4 Comments

I think you mean //shapes.cpp bool Shapes::Draw() { } instead of this //shapes.cpp virtual bool Shapes::Draw() { }
It works now but why do I have to declare it, this seems a bit silly it should have a reference from the class it inherited from.
@WinterGreen but you declared the method Draw as pure virtual by specifying the =0 at the end, this mean that you force any inheriting class to define the method.
Oops! virtual keyword should not be use in method definition. Corrected it.
0

Your return types are not matching. The Renderable::Draw returns bool, and you Shapes::Draw returns void. The entire function signature (including the return types) must match, otherwise the function in the derived class simply hides the function in the base class.

2 Comments

Actually the return type does not need to be an exact match, rules are relaxed for covariant types.
@WinterGreen: Could you please post a complete, executable example that demonstrates what is not working?
0

You need to declare Draw in Shapes:

class Shapes : public Entity, public Renderable
{
  public:
    Shapes();
    ~Shapes();
    virtual void Draw(); 
};

Just defining it later is not enough.

1 Comment

Thanks this worked but The since Renderable is virtual void Draw()=0; I just have to redefine it in the class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.