Sign in to follow this  
ricekrispyw

Accesing overridden parent functions

Recommended Posts

ricekrispyw    388
If I have this:
class A
{
public: 
    void askQuestion()
    {
        //do stuff
    }
};

class B : public A
{
public:
    void askQuestion()
    {
        //I WANT TO CALL A'S FUNCTION!!!!!!
        //do more stuff
    }
};

How do I call A's askQuestion from B's? I read that it's like
A::askQuestion();
, but VC++ gives me errors about trying to access a non-static function and bla, bla, bla. Thanks.

Share this post


Link to post
Share on other sites
Andrew Russell    1394
You have to call it like this - with this [wink]

this->A::askQuestion();
Your compiler is correct, just going A::askQuestion() is for accessing static functions.


EDIT:
I just noticed glat edited his post to match mine. His previous post was correct though - you can also do this externally by doing this (this is because it's public):

B foo;
foo.A::askQuestion();

Share this post


Link to post
Share on other sites
JohnBolton    1372
Hmm, "this->A::askQuestion()" and not "A::askQuestion()"?

That must be a recent change to the standard. I have always left off the "this->" and it compiled just fine. In fact, Stroustrup 3rd ed. says to use "A::askQuestion()".

Share this post


Link to post
Share on other sites
ricekrispyw    388
Quote:
Original post by JohnBolton
Hmm, "this->A::askQuestion()" and not "A::askQuestion()"?

That must be a recent change to the standard. I have always left off the "this->" and it compiled just fine. In fact, Stroustrup 3rd ed. says to use "A::askQuestion()".


That's what Dietel said, too. I still have to check the other out, though. I've been working on something else.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this