Sign in to follow this  

Accesing overridden parent functions

This topic is 4544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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
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
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

This topic is 4544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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