S
stefven blonqhern
hello all, having a problem with derived classes. i'll show by
pseudo code example. first the base class and derived classes:
class Shape { // base class for all shapes
public:
virtual void Draw() {// draw code here}
};
class Square : public Shape {// etc};
class Circle : public Shape {// etc};
class Triangle : public Shape {// etc};
then I create another class called MyClass which has a member function
ChangeShape which creates a new Shape object and assigns a pointer to
it. the member function Render is being called constantly in the
background.
class MyClass {
public:
void ChangeShape(int shape) {
// if shape = 0 then sh = Square, 1 = Circle, 2 = Triangle
// but for the sake of brevity i'll just use a square
sh = Square();
shPtr = □
}
void Render() {shPtr->Draw};
Shape sh;
Shape* shPtr;
};
obviously whenever i access the member function Draw through the
Shape* (shPtr) it always calls the member for Shape not Square because
sh is of type Shape. all this might sound ridiculous? can i assign
derived classes to sh and keep the derived class type instead of them
being converted to the base class type? my implementation is somewhat
limited by the plug-in architecture i must work within.
TIA for any help, hope i explained that clearly.
Stephen.
pseudo code example. first the base class and derived classes:
class Shape { // base class for all shapes
public:
virtual void Draw() {// draw code here}
};
class Square : public Shape {// etc};
class Circle : public Shape {// etc};
class Triangle : public Shape {// etc};
then I create another class called MyClass which has a member function
ChangeShape which creates a new Shape object and assigns a pointer to
it. the member function Render is being called constantly in the
background.
class MyClass {
public:
void ChangeShape(int shape) {
// if shape = 0 then sh = Square, 1 = Circle, 2 = Triangle
// but for the sake of brevity i'll just use a square
sh = Square();
shPtr = □
}
void Render() {shPtr->Draw};
Shape sh;
Shape* shPtr;
};
obviously whenever i access the member function Draw through the
Shape* (shPtr) it always calls the member for Shape not Square because
sh is of type Shape. all this might sound ridiculous? can i assign
derived classes to sh and keep the derived class type instead of them
being converted to the base class type? my implementation is somewhat
limited by the plug-in architecture i must work within.
TIA for any help, hope i explained that clearly.
Stephen.