0

I want to write a help function which calls a class method:

foo.h

class cls{
  public:
  void fun();
  void fun1();
};

foo.cpp

void cls::fun(){
  helperFun();
}

void helperFun(){
  fun1();//how to call fun1() here?
}

How should I do it, or what's the best way of doing it?

Xun Yang
  • 4,209
  • 8
  • 39
  • 68

5 Answers5

6

In order to call a method of cls you need to have a cls instance at hand. fun has access to this and can use it to provide helperFun with that instance:

void cls::fun(){
  helperFun(*this);
}

// If fun1() was const, the parameter could be a const cls&
void helperFun(cls& arg){
  arg.fun1();
}

This is only one (pretty direct) way of arranging things, there are more options in general and picking the best depends on what you are trying to do exactly.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • 3
    Nice answer. Looks familiar! :P – Joseph Mansfield Apr 26 '13 at 08:52
  • 1
    One of you guys should change your answer to use a pointer instead of a reference. You know... for variety! But decide which one ahead of time, lest we end with two same answers again ;) – Nik Bougalis Apr 26 '13 at 08:53
  • @sftrabbit: And to think I thought of naming the argument `cls` and then `obj` before deciding on `arg`... :-) – Jon Apr 26 '13 at 08:53
4

It needs an instance of the class to be called on. Perhaps you want the instance to be the same as the one that fun is being called on. In which case, you could pass *this to helperFun:

void cls::fun(){
  helperFun(*this);
}

void helperFun(cls& obj){
  obj.fun1();
}
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
1

Pass helper function as argument to fun1:

void cls::fun(){
  helperFun(this); //currently passing as this pointer
}

void helperFun(cls* obj){
  obj->fun1();
}
shivakumar
  • 3,297
  • 19
  • 28
1

A non-static class method, such as fun, is always called on an instance of its class. You need an instance of cls to call fun. You may get that instance in one of the following ways:

  • By creating a cls object inside the body of fun.
  • By passing a cls object (or a reference to it, or a pointer to it) as a parameter to fun (involves changing the signature of fun).
  • By creating a global variable of type cls (I strongly discourage this option).

Alternatively, fun might be declared as static, provided that it does not use any filed of the cls class. In that case, you could invoke it without an associated instance of cls, with the following instruction: cls::fun().

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
  • If cls is used as a singleton, should option 3 be less discouraged? – Xun Yang Apr 26 '13 at 08:59
  • @Xun Yan Maybe, but I've never used the Singleton Pattern, so I can't really talk about it. – Daniel Daranas Apr 26 '13 at 09:03
  • OK, basically I made a extern object of cls inside the library, so people don't need to initialize cls objects themselves, but rather referring to the object all the time. It may not be singleton, but as cls is only initialized once, I guess it's not a problem... – Xun Yang Apr 26 '13 at 09:11
0

If you need a helper function that you call from one method of the class, and then it needs to call into another method of that same class, why don't you make it a member of the class? I mean, it cannot be a stand-alone method, it is tightly coupled with your class. So it must belong to the class, IMO.

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
  • It's a good point, but I also worry about the tidiness of .h file, so keeping the one-time helper function in .cpp make it easier to read IMO – Xun Yang Apr 26 '13 at 09:06
  • 2
    I was going to say that Scott Meyers disagrees and he says we should [prefer non-member non-friend functions](http://www.drdobbs.com/cpp/how-non-member-functions-improve-encapsu/184401197) whenever possible. But since the helper function is called from the class, and it calls other functions inside the class, I think you're right in saying it should be part of the class. – MikMik Apr 26 '13 at 09:08