Ok is there any why i can call the base class this pointer or object from with in a derived class? Example:

class BASE 
{
   public:
      BASE () {}
      BASE(BASE & b){}
      virtual ~BASE () {}
      virtual void Method(){cout << "BASE::Method called"; return;    }
};

class DERIVED: public BASE
{
   public:
      DERIVED() {}
      DERIVED (DERIVED & D){}
      ~DERIVED()
      void Method() { cout << "Derived::Method called"; return; }
};

int main() 
{
DERIVED derive;
BASE * base = NULL;

base = new DERIVED(deriveObj);
base ->Method();

}

so within tha derived class i would have a function like

trysomething()
{
BASE::BASE(BASE::this);
}
????

Recommended Answers

All 4 Replies

nosibar: acceptable code tags are [code=cplusplus] ... [/code]

The way to call base class from derived

void DERIVED::Method()
{
     BASE::Method();  // call Method() is base class.
}

Note that its not necessary to pass the this pointer because that is passed by default.

Yes I know that.
That is how you access the methods.
But what if the method takes that base class as a parameter and you want to call that method from the derived class?

What I actually want to do is to reference the entire object set rather than individual members or methods.
Rather than, individual data member

BASE::getadatamember();
BASE::getanotherdatamamber();
BASE::getetcdatamameber();

want to reference the entire BASE object. possible?

If you look at the way in which the objects are laid out in memory, BASE and DERIVED pointers to the same DERIVED object have the same address (unless there is multiple inheritance involved). So you can access your BASE object from the DERIVED pointer.

If you can do

trysomething()
{
BASE::BASE(BASE::this);
}

In your BASE class function a *this will give a BASE object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.