-1

I got into the scenario where I need to call member function from member variable of different class.

Example:

class B {
  void foo() {
    // Need to call bar()
  }
};

class A {
  B b;
  void abc() {
    b.foo();
  }
  void bar() {}
};

I need to call bar() method from foo(). One way of doing is passing this to foo() function, but that I think is inefficient way of doing it.

Please suggest any efficient ways of doing it. I am interested to learn how people follow different approach to solve this.

halfer
  • 19,824
  • 17
  • 99
  • 186
sudo
  • 548
  • 1
  • 4
  • 16
  • 7
    Well, for one, `B.foo();` should be `b.foo();`. Apart from that, somehow the instance of `A` on which you're calling `abc()` needs to make it's presence known to `foo`, somehow. Not going to sugarcoat it: the code smell of this design is raging-pungent, so maybe think about it some more. – WhozCraig Jan 18 '20 at 11:42
  • Sorry it was mistake from my side. It is infact `b.foo()`. I wanted to learn the design choice people make that's why I posted the question. – sudo Jan 18 '20 at 11:45
  • 1
    To call a function on an object, you *need* an instance of said object to invoke it on (unless it is `static`). – Jesper Juhl Jan 18 '20 at 13:28

2 Answers2

0

Is there another way to solve your problem without doing that? Because even if you manage to do this, it would be a bad coding practice.

But if there's not another way, it's a little better to make foo() a static method. So you can call it by B::foo()

isocppforbids
  • 389
  • 1
  • 12
0

There generally exist two different kinds of functions.

One kind, that's static member functions, global functions, or global functions in a namespace. They do not reference or otherwise depend on members of a class instance. You can call these from one or another class, no problem.

The other kind is the (non-static) member function. It does in some way reference, access, change, use, or otherwise depend on something that is a class member. If you want to call such a function, you need the this pointer.
When calling a function from "inside" the class itself, then this is implicit. So, if you type e.g. bar() inside A::abc, then that's really this->bar(), only without you needing to type that.
From "outside", that is e.g. in B::foo, you will need to supply an object, or else the compiler has no way of knowing what data to operate on. It does not, and cannot work without.

So, you need to decide whether whatever happens in bar depends on members of A or not. If it does, you have no choice. Passing an object pointer (reference) is not about being inefficient, it's about "possible or not possible".

Otherwise, if there is maybe only some common functionality that is used by both B::foo and A::bar, in which case you could move that functionality to a (namespaced) global function or a non-capturing lambda, or anything the like.
You could thus for example call detail::blah() from both B::foo and A::bar without needing to pass an object. That way, you would not need to duplicate code.

Or well, make A::bar static, which will also work but in my opinion has no advantage, and is less clear (as it somehow makes the function a part of A, logically, when in reality that's not truly the case).

Damon
  • 67,688
  • 20
  • 135
  • 185