Jump to content

  • Log In with Google      Sign In   
  • Create Account

multiple base classes with virtual functions.


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
5 replies to this topic

#1   Members   

277
Like
0Likes
Like

Posted 04 June 2013 - 11:24 PM

OK, apparently you cannot create a derived class from a base class if both classes have polymorphic functions. for example:

 

Base Class:

class A
{
public:
    A(){}
    
    ~A(){}
    
    virtual void init() = 0;
   
    virtual void deleted() = 0;
};

void A::init()
{
}

void A::deleted()
{
}
 

 

 Derived class one:

class B : public A
{
public: 
    B(){}

    ~B(){}
   
    virtual void init() = 0;

    virtual void deleted() = 0;
}

void B::init()
{}

void B::deleted()
{}

Derived Class # 2

 

class C : public B
{
public:
    C(){}

    ~C(){}

    void init();

    void deleted();
};

void C::init()
{}

void C::deleted()
{}

program:

 

int main()
{
   C* c = new B();
}

this will produce errors saying how you can't instaciate B because of the virtual functions in both A and B but this needs to work because I would make render specific resource and has to be able to use something like:

 

class IResource
{
  public:
     // virtual functions common to all resources.
}

class IHardwareMaterial : public IResource
{
public:
    // virtual functions related to a hardware material.
}

class ID3D9HardwareMaterial : IHardwareMaterial
{}

I'm not sure if I explained my self well. This is all pseudo code, sort of so I'm not sure if I'm communicating this right.



#2   Members   

1711
Like
5Likes
Like

Posted 04 June 2013 - 11:45 PM

*
POPULAR

B (or A) is not a C so doing 'C *c = new B;' cannot cast a B to a C pointer.

Perhaps you meant to do 'B *b = new C;' ? 



#3   Members   

1093
Like
0Likes
Like

Posted 05 June 2013 - 01:58 AM

You can either implement you methods or declare them pure virtual, but not both.



#4   Members   

2536
Like
0Likes
Like

Posted 05 June 2013 - 04:53 AM


class B : public A

{

public:

  B(){}

  ~B(){}

  virtual void init();  //< do not declare these are pure virtual! This only needs to happen in A.

  virtual void deleted();

}


void B::init() //< because you're declaring the implementations here!

{}

void B::deleted()

{}


Edited by RobTheBloke, 05 June 2013 - 04:54 AM.


#5   Members   

2536
Like
0Likes
Like

Posted 05 June 2013 - 04:55 AM

p.s. This will not work!


int main()

{

   C* c = new B();

}



However this will .....


int main()

{

   A* a = new B();

}


Edited by RobTheBloke, 05 June 2013 - 04:55 AM.


#6   Prime Members   

3010
Like
1Likes
Like

Posted 06 June 2013 - 04:13 PM

You can either implement you methods or declare them pure virtual, but not both.

 

Actually that's not true.  A pure virtual function can have an implementation, you just can't instantiate a class with a pure virtual function.  You can still call the pure virtual function from a derived class.

 

They covered it in more detail here: http://stackoverflow.com/questions/2089083/pure-virtual-function-with-implementation






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.