Accessing Base class function using a pointer to a derived class

A

Abhijit Deshpande

Is there any elegant way to acheive following:

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

class Derived : public Base {
public:
Derived() {}
~Derived()
void Method() { cout << "Derived::Method called"; return; }
};

int main() {
Derived deriveObj;
Base * basePtr = 0;

basePtr = <some kind of cast????> &deriveObj;

basePtr->Method();
}

In the above code, the call "basePtr->Method" should print
"Base::Method called" and not "Derived::Method called".

Is there any way to assign address of "deriveObj" to "basePtr" so that
the virtual mechanism is bypassed and call to member function "Method"
actually calls the member function from the "class Base" and not from
"class Derived".

Thanks in advance,
Regards,
Abhijit.
 
R

Rolf Magnus

Abhijit said:
Is there any elegant way to acheive following:

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

class Derived : public Base {
public:
Derived() {}
~Derived()
void Method() { cout << "Derived::Method called"; return; }
};

int main() {
Derived deriveObj;
Base * basePtr = 0;

basePtr = <some kind of cast????> &deriveObj;

basePtr->Method();
}

In the above code, the call "basePtr->Method" should print
"Base::Method called" and not "Derived::Method called".

Is there any way to assign address of "deriveObj" to "basePtr" so that
the virtual mechanism is bypassed and call to member function "Method"
actually calls the member function from the "class Base" and not from
"class Derived".

basePtr->Base::Method();

But please think thrice before doing that. Looks like bad design to me.
 
J

John Harrison

Abhijit Deshpande said:
Is there any elegant way to acheive following:

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

class Derived : public Base {
public:
Derived() {}
~Derived()
void Method() { cout << "Derived::Method called"; return; }
};

int main() {
Derived deriveObj;
Base * basePtr = 0;

basePtr = <some kind of cast????> &deriveObj;

basePtr->Method();
}

In the above code, the call "basePtr->Method" should print
"Base::Method called" and not "Derived::Method called".

Is there any way to assign address of "deriveObj" to "basePtr" so that
the virtual mechanism is bypassed and call to member function "Method"
actually calls the member function from the "class Base" and not from
"class Derived".

Thanks in advance,
Regards,
Abhijit.

You can do this

Derived deriveObj;
Base * basePtr = 0;

basePtr = &deriveObj;

basePtr->Base::Method(); // calls Base::Method

but there is no way to bypass the virtual machanism when you assign a
pointer.

john
 
J

John Harrison

Craig Thomson said:
Hi,


I'm a little hazy on the whole casting business, but as I understand it what
you want is:

main() {
Derived deriveObj;
Base * basePtr = 0;

basePtr = dynamic_cast<Base*>(&DeriveObj);

basePtr->Method();
}

or

main() {
Derived deriveObj;
Base * basePtr = 0;

basePtr = static_cast<Base*>(&DeriveObj);

basePtr->Method();
}

Dynamic cast is safer because it does run time type checking but as long as
your going from derived to base class static cast should work fine too.
Google for dynamic_cast or static_cast and you should get all the
information you need.

Neither type of cast is necessary when converting from a derived class
pointer to a base class pointer, and neither cast achieves what the OP wants
which is to override the virtual function calling mechanism.

You've got this mixed up with converting from a base class pointer to a
derived class pointer, when some sort of cast is necessary.

john
 
A

Abhijit Deshpande

Thanks John and Ralf for the solution.

But I was wondering, should following piece of code work?
In addition to "class Base" and "class Derived", we define one more
class,

class DummyBase() {

public:

DummyBase() {
}

~DummyBase() {
}

void Method() {
Base::Method();

return;
}
};

int main() {
Derived deriveObj;
DummyBase * dummyBasePtr = reinterpret_cast<DummyBase
*>(&deriveObj);

dummyBasePtr->Method();

return 0;
}

This should print "Base::Method called". Is there anything
conceptually wrong in above piece of code?? Because, I tried it using
GNU g++ on RedHat linux 7.2, and it still prints "Derived::Method
called".

Regards,
Abhijit.
 
K

Karl Heinz Buchegger

Abhijit said:
Thanks John and Ralf for the solution.

But I was wondering, should following piece of code work?
In addition to "class Base" and "class Derived", we define one more
class,

class DummyBase() {
public:

DummyBase() {
}

~DummyBase() {
}

void Method() {
Base::Method();

return;
}
};

int main() {
Derived deriveObj;
DummyBase * dummyBasePtr = reinterpret_cast<DummyBase
*>(&deriveObj);

You are casting way to much!
What (if any) is the relationship of Derived and DummyBase.
Please show it with code and not with english descriptions.

dummyBasePtr->Method();

return 0;
}

This should print "Base::Method called". Is there anything
conceptually wrong in above piece of code??

Impossible to say without seeing the actual, complete code you used to test it.
 
A

Abhijit Deshpande

Well, here is the complete piece of code, I tried..

class Base {

public:

Base() {
};

virtual ~Base() {
};

virtual void Method() {
printf("Base::Method called.\n");

return;
}
};

class Derived : public Base {

public:

Derived() {
};

~Derived() {
};

void Method() {
printf("Derived::Method called.\n");

return;
}
};

class DummyBase : public Base {

public:

DummyBase() {
}

~DummyBase() {
}

void Method() {
Base::Method();

return;
}
};

int main() {

Derived derivedObj;
DummyBase *dummyBasePtr;

dummyBasePtr = reinterpret_cast<DummyBase *>(&derivedObj);

dummyBasePtr->Method();
}

And the expected output is "Base::Method called.", whereas the actual
output is "Derived::Method called."

Regards,
Abhijit.
 
Joined
Jul 16, 2008
Messages
1
Reaction score
0
If your "Method" is virtual its going to call the derived method

Since your Base class "Method" is virtual its going to call the derived method and expected output will be Derived method and not the base Method. Try removing virtual from Base Method and you should see what you want to see.:captain:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top