Inheritance question
I have:
Code:
class A { Code:
class B: public A { Code:
class C { Code:
void C::Update () { Code:
class D: public C { Is there any way to accomplish this? And I don't want to use C::Update (A* a) which will defenitely work if I pass B* a to it. Thanks. |
I'm not sure I understand
why don't you override it??? .. Declare and define a D::Update .. not that's a very good desing .. but .. |
yeh, override the update() function in D..
in C, make Update() virtual then in D, define an Update() |
Ok, this is kinda ugly, but it does what I want. Overriding is what I escape since I want to have one base class that contains common functions so that I can use them from derived classes.
Code:
#include <iostream> |
Hmm thats nasty code, heres a few points:
Any class which is a base for another class should have a virtual destructor. You create a D which calls the constructor C which creates a pointer. D then creates a new pointer and assigns it to the pointer created by C; Memory leak. There are two copies of the pointer in two classes which is responsible for the destruction? Neither clean up memory leak. |
Hmm... I wrote some Java code that solve problem. But I didn't understand why upcast isn't work in C++.
Code:
public class A { |
> But I didn't understand why upcast isn't work in C++.
you cant cast from an A to a B, a B to an A sure, but not an A to a B.. :) edit> oops. sorry i didnt see in D where you say a = new B(), i take back my previous statement, your cast should work.. |
All times are GMT -5. The time now is 07:37 AM. |