views:

1164

answers:

8

I was asked this crazy question. I was out of my wits.

Can a method in base class which is declared as virtual be called using the base class pointer which is pointing to a derived class object?

Is this possible?

+34  A: 

If you're trying to invoke a virtual method from the base class pointer, yes.

That's polymorphism.

If you're asking, with a base class pointer to a derived class, can you invoke a base class method that is overriden by the derived class? Yes that's also possible by explicitly scoping the base class name:

basePtr->BaseClass::myMethod();

Alan
Polymorphism is where it class the derived class method because the method is virtual in the base class. Actually calling the base class method via a pointer to base even if the base class method has been overridden is bypassing polymorphism which is different.
Charles Bailey
Actually it's the opposite. The question is asking to call the base class's virtual method when the object is of the derived type which overrides the method.
JaredPar
Hrm, the question as written was confusing.
Alan
Or I fail at reading comprehension :D
Alan
Hmm, I never knew this was an option (guess I never needed it). Thanks!
Uri
+9  A: 

You mean something like this. (Where pBase is of type pointer-to-base but the pointed-to object is actually of type Derived which is derived from Base.)

pBase->Base::method();

Yes, it's possible.

Charles Bailey
+12  A: 

Try:

class A            { virtual void foo(); }
class B : public A { virtual void foo(); }

A *b = new B();
b->A::foo ();
el.pescado
Actually I think it should be "A *b = new B()", instead of "B *b = new B()", to follow the question.
Luca
Yeah, it should.
el.pescado
A: 

No. Not in a clean way. But yes. You have to do some pointer manipulation, obtain a pointer to the vtable and make a call. but that is not exactly a pointer to base class, but some smart pointer manipulation. Another approach is using scope resolution operator on base class.

Sudesh Sawant
C++ does support directly what the question asks. `base->base::function()` would do the trick. No need to vtable hackery here.
John Dibling
Re-read your post. "scope resolution operator" is not "another approach." It is the only approach you should be considering.
John Dibling
I agree. Scope Resolution is the way..
Sudesh Sawant
+9  A: 

Yes -- you have to specify the full name though:

#include <iostream>

struct base { 
    virtual void print() { std::cout << "Base"; }
};

struct derived : base {
    virtual void print() { std::cout << "Derived"; }
};

int main() { 
    base *b = new derived;
    b->base::print();
    delete b;
    return 0;
}
Jerry Coffin
+5  A: 

If I understand the question correctly, you have

class B 
{
public: 
    virtual void foo();
};

class D: public B
{
public:
    virtual void foo();
}

B* b = new D;

And the question is, can you call B::foo(). The answer is yes, using

b->B::foo()
KeithB
A: 

class Dictionary { environment,or how to better perfomance.

jose
Are "answers" like this even worth a down vote?
franji1
If I could downvote this twice, I would. Once for making my eyes bleed, and once for just posting random questions as answers.
John Dibling
A: 
class B 
{
public: 
    virtual void foo();
};

class D: public B
{
public:
    virtual void foo();
}

B* b = new D;

Try calling

(*b).foo()

to invoke base class foo function

Pardeep
@pardeep are you sure that this will call base class foo function?did you test it?
Vijay Sarathi
It was my general assumption that (*b) will eventually have proper object but on compile and run I found that this is not the case. It still calls derived function.This has created confusion for me also that what is the difference between(*b).foo()andB b1 = *b;b1.foo()
Pardeep