0

I think only the static method can do the following thing, but it can works. can anybody tell me how it works? what's the principle behind this thing.

#include <iostream>
using namespace std;

class Parent {
protected:
    unsigned char* buf;
    unsigned int bufLenght;

public:
     void Setup()
    {
        buf = nullptr;
        bufLenght = 0;
        cout << "in Parent class Setup()" << endl;
    }
    virtual void TearDown() 
    {
        delete[] buf;
    }
};

class Child : public Parent{
public:
    virtual void Setup()
    {
        Parent::Setup();    // access Parent method without a parent's object?
        cout << "in Child class Setup()" << endl;
    }
};

int main(int argc, char const *argv[])
{
    Child co;
    co.Setup();

    return 0;
}

run this code, the result is :

in Parent class Setup()

in Child class Setup()

I find the answer here: How to call a parent class function from derived class function?

and in thinking in c++, I also find the same description: However, when you’re redefining a function, you may still want to call the base-class version. If, inside set( ), you simply call set( ) you’ll get the local version of the function – a recursive function call. To call the base-class version, you must explicitly name the base class using the scope resolution operator.

  • 1
    No, this does not "access parent method without a parent's object". This invokes the parent's method on `this` object. – Sam Varshavchik Oct 30 '18 at 01:19
  • but "Parent::Setup();" is in the parent class. Only static method can access without an object. – irwin.jiangwei Oct 30 '18 at 03:22
  • @SamVarshavchik, thank you for your reply. I'm still confused. this object here belongs to Child class, not the Parent class. now we are calling Parent Setup method. – irwin.jiangwei Oct 30 '18 at 04:24

2 Answers2

1

Each Child object is built on top of a Parent object. Whenever you have a Child you also have a Parent.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • @Peter, yes I know. what you say can be done: subclass object.xxx(parent method). but here no object, just 'Parent:: Setup()' – irwin.jiangwei Oct 30 '18 at 04:30
0

I can't seem to understand what you're trying to achieve. It appears that you've omitted the 'virtual' keyword on the base class method you're trying to override and hence receiving errors from the compiler.

Although your question is fairly unclear, here is my best attempt at demonstrating how to implement polymorphism in C++:

class A {
protected:
    // You will not be able to access this in the
    // other class unless you explicitly declare it as
    // a 'friend' class.
    int m_ProtectedVariable;

public:
    // Let's define a virtual function that we can
    // override in another class.
    virtual void ClassMethod( ) {
        printf( "[A::ClassMethod] Called!\n" );
    }
}

class B : public A {
public:
    // There is no need for the virtual/override keywords
    // if you are overloading the function which is already defined
    // in another class as 'virtual'. I prefer to keep them for
    // pedantic reasons.
    /* virtual */ void ClassMethod( ) /* override */ {
        // 
        printf( "[B::ClassMethod] Called!\n" );

        // Since the function is a virtual, we can always
        // call the base class function.
        A::ClassMethod( /* ... */ );
    }
}

Hopefully you find this helpful in whatever you're trying to achieve :-)

EDIT: In your particular scenario, where you're supposed to allocate a buffer when you need it and destroy it afterwards - why are you not making use of the class constructor/destructor functionality? It would be far more intuitive to let the compiler decide when to manage memory (in this case) as it will happen automatically once your object goes out of scope.