50

I wrote a method (that works fine) for a() in a class. I want to write another method in that class that calls the first method so:

void A::a() {
  do_stuff;
}

void A::b() {
  a();
  do_stuff;
}

I suppose I could just rewrite b() so b(A obj) but I don't want to. In java can you do something like this.a().

I want to do obj.b() where obj.a() would be called as a result of obj.b().

5
  • 2
    Can you clarify what you want? Commented Jun 11, 2009 at 20:07
  • oh whoops! I had a different error causing this -- which I just fixed. Commented Jun 11, 2009 at 20:12
  • 5
    make sure a is defined before b, if b is calling a Commented May 26, 2016 at 4:15
  • If there is a variable in A::a() then how to access that variable in A::b() ? Commented Jan 28, 2021 at 7:03
  • @Darshan, If you meant a local variable in A:a() then you can't access it in A::b(). If you meant a class member, then you access it in the same way in any class method, by using its identifier. Commented Oct 23, 2023 at 9:53

5 Answers 5

74

What you have should work fine. You can use "this" if you want to:

void A::b() {
  this->a();
  do_stuff;
}

or

void A::b() {
  this->A::a();
  do_stuff;
}

or

void A::b() {
  A::a();
  do_stuff;
}

but what you have should also work:

void A::b() {
  a();
  do_stuff;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I get an error: invalid use of 'this' in non-member function
is it a better practice to use this when referencing methods/properties of the class?
If there is a variable in A::a() then how to access that variable in A::b() ?
32

That's exactly what you are doing.

1 Comment

The answer ^^^ here is not useful; the one below with +44 points is a LOT more insightful. I recommend to change the order in the long run. I will explain why: I came to this here via Google, and the answer was not helping me - but the answer below WITH the syntax made me realize that I simply forgot a (). So the answer below with the +44 points was more helpful for my case, whereas the "what you are doing" comment adds really almost nothing at all to it.
5

It looks like the code you wrote in your block would work just fine. Just make sure you have both the a() and b() methods defined inside your class properly.

Comments

5

There's one case in which you might have slightly unexpected results. That is if A::a() is virtual, obj actually has type DerivedFromA, and DerivedFromA::a overrides A::a. In that case, the simple call a(); or the more verbose this->a(); will not call A::a but DerivedFromA::a().

Now, this is probably intended, since class A declared a() to be virtual. But if you really don't mean it, you can ignore the virtual by writing the call either as

void A::b()
{
    A::a(); // or
    this->A::a(); //Both ignore the virtual-ness of a()
}    

Comments

4

What you have written there should work fine. In C++ if you call a within b and both are instance methods of some class A, then you don't need to qualify it. Both a and b are in each others' scope.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.