0

In a method of an object of type class A, I handle an object of class B that has a public method .join(*A).

I want my object of type A calling this someObjectOfTypeB.join(*A) method, to use a pointer to itself as the parameter.

void A::someMethod()
{
    B b();
    b.join(I want to a to use a pointer to itself as a parameter);
}

A a();
a.someMethod();

Upon further investigation, this was not the problem as I led myself to believe; and is indeed the correct way of doing what I wanted to do.

Werner
  • 769
  • 2
  • 6
  • 19
  • 1
    Isn't that what `this` was purposed for!?! – πάντα ῥεῖ Nov 23 '14 at 01:54
  • 2
    You are declaring `b` as a _function_ here, taking void and returning a `B`. Is this what you want? – Andrew Lazarus Nov 23 '14 at 01:58
  • `*A` is not a valid type. Did you mean `A*` for `join`'s parameter type, or did you mean that given a suitable expression `A`, `join` can be called as `join(*A)`? –  Nov 23 '14 at 02:05
  • @JonathanWakely: If you actually read that article you'll see that this is _not_ the most vexing parse, and I wish people would stop spreading the myth that it is. It is merely related to the MVP. – Lightness Races in Orbit Nov 23 '14 at 02:11
  • @Werner, I suspect you have got confused by the error message due to declaring `B b()`, which does not do what you think it does (as the comment above says, it declares a function). Try `B b; b.join(this);` (N.B. that's `B b;` _not_ `B b();`) – Jonathan Wakely Nov 23 '14 at 02:15
  • @LightnessRacesinOrbit, I didn't say it _is_ the MVP, I just pointed to the wikipedia page which covers similar cases, but I've replaced the comment anyway. Do you have a better reference (such as an SO answer) for "that's not a default-initialization it's a function declaration"? – Jonathan Wakely Nov 23 '14 at 02:16
  • @JonathanWakely: Unfortunately, silly people keep closing them as dupes of confused MVP questions, but http://stackoverflow.com/questions/180172/default-constructor-with-empty-brackets isn't too bad if you ignore the constant propagation of the same myth throughout its answers. – Lightness Races in Orbit Nov 23 '14 at 02:31

1 Answers1

6

Try using this:

void A::someMethod()
{
    B b;
    b.join(this);
}

As @AndrewLazarus and @JonathanWakely commented, use B b; instead of B b(). The later declares a function b without parameters which returns B, and that is not what you want.

AlexD
  • 32,156
  • 3
  • 71
  • 65