• Advertisement
Sign in to follow this  

Accesing overridden parent functions

This topic is 4614 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
Advertisement
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
OK, thanks.

It looked wrong to me (the way the book said), but as I clearly didn't know what was right, I had no basis to go on?

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
Yeah, it should work without the this.

Share this post


Link to post
Share on other sites
Unless you also have code somewhere as follows:

namespace A
{
void askQuestion()
{
//Muauahahahahah
}
}

namespace SomethingElse
{
class A
{
void askQuestion();
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement