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?

4
  • 2
    fun and fun1 are private? Commented Apr 26, 2013 at 8:50
  • 1
    Pass a reference to the class to the helper function? Put the helper function inside your class? Commented Apr 26, 2013 at 8:51
  • Doesn't matter, I can make them public :) Commented Apr 26, 2013 at 8:51
  • 2
    You need an instance of cls in order to call fun1(). You also need to make it public. Commented Apr 26, 2013 at 8:51

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Nice answer. Looks familiar! :P
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 ;)
@sftrabbit: And to think I thought of naming the argument cls and then obj before deciding on arg... :-)
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(); } 

Comments

1

Pass helper function as argument to fun1:

void cls::fun(){ helperFun(this); //currently passing as this pointer } void helperFun(cls* obj){ obj->fun1(); } 

Comments

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().

3 Comments

If cls is used as a singleton, should option 3 be less discouraged?
@Xun Yan Maybe, but I've never used the Singleton Pattern, so I can't really talk about it.
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...
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.

2 Comments

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
I was going to say that Scott Meyers disagrees and he says we should prefer non-member non-friend functions 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.