tags:

views:

128

answers:

4

I would like to make somekind of alias in c++ to single tone calling so instead of calling each time to MYCLASS::GetInstance()->someFunction(); I could call just someFunctionAlias(); in my code .

+2  A: 

Use a static function.

namespace ... {
    void someFunction() {
        MYCLASS::GetInstance()->someFunction();
    }
};

Edit: Sorry lads, I wrote static someFunction and meant void someFunction.

DeadMG
You're missing a return type.
Bertrand Marron
Putting this as an inline function may save code size in the executable.
Johannes Schaub - litb
@Johannes: aren't compilers ignoring the `inline` statement?
Default
@Michael - Err, no?
mathepic
The only downside to using `inline` is that it reduces API flexibility. If you're always able to rebuild the callers of the code when you change the API, that's not a big deal. But if you're trying to maintain a stable API so that callers don't need to be updated every time (e.g., because it's a public interface to a library) then it's better to not use `inline` in that interface at all. (I've regretted doing the same thing with `#define` in C, FWIW. It's the same issue really, but with different details.)
Donal Fellows
@Donal that's true not only for the `inline` keyword but of *any* function whose code is visible to the caller. The same problem happens when you use `static` and put that function definition into the header. The only guaranteed to be observable difference when putting `inline` instead of `static` is that the out-of-line code for the function won't be duplicated for every translation unit that causes it to be emitted (by taking its address for example).
Johannes Schaub - litb
@mathepic: according to this answer, it does: http://stackoverflow.com/questions/1204975/does-the-compiler-decide-when-to-inline-my-functions-in-c/1204984#1204984
Default
@Johannes: The point is to consider whether the clients of the API should be strongly bound to the implementation of the API. If it's an API that is internal to the application, then sure, why not? External APIs though, they should always reduce coupling as much as possible (it's going to be a problem anyway, but making it worse is dumb). Finally, `inline` is one of many ways to couple a client of an API to the implementation of that API; the performance boost is a consequence of the trade-off.
Donal Fellows
@Donal well, then I suspect I misunderstood you. I thought you were talking about the downside of replacing static by inline in the above code. To be fair, that conclusion wasn't all too implausible, given the aforementioned statements by me, @Michael and @mathepic. I agree fully with you that to reduce coupling, the code of function should not be put into the header. But as I read it, this is what @DeadMG's code was intended to do. In such a case, `inline` is clearly the better choice as it reduces code duplication. Cheers :)
Johannes Schaub - litb
@Johannes: I'm not criticizing at all. Just noting that there's an *essential* trade-off. :-)
Donal Fellows
@Michael you are misinterpreting it under the wrong assumption that it is just a recommendation to the compiler to inline calls to it. "inline" has more effects. These should be explained in any good C++ book. It is like saying that "const" in C++ is ignored just because the variables aren't placed into read-only memory.
Johannes Schaub - litb
@Johannes Schaub - litb: well, unfortunately I do not have any good C++ books around.. But I did find this msdn article as well stating that "The insertion [] occurs only if the compiler's cost/benefit analysis show it to be profitable.": http://msdn.microsoft.com/en-us/library/z8y1yy88.aspx this is why I thought the compiler ignored it. Also, on a sidenote I thought the compiler was smart enough do to automatic insertion where it saw fit (without specifying it myself). But that could be wrong. Also, (since I don't have the C++ book) I did not get your `const` similarity. Sry 'bout that
Default
@Michael you don't need to be sorry about anything. For the `const` example: You still think that `inline`'s only effect is to recommend the compiler to inline calls. Just like someone would say "the only effect const has is to place the variable into read-only memory", you say "the only effect inline has is to recommend compilers to inline calls to it". Both is not true. "const" makes the compiler forbid writes to it in addition. And "inline" allows you to define the function multiple times in the program. A compiler cannot just say "Oh, i ignore 'inline', and don't allow users to do that!".
Johannes Schaub - litb
@Johannes: well, the only way that the executable is going to be smaller is if the call is inlined, aka substituted into the code. And this operation does not have anything to do with multiple definitions (which I am trying to find references on: do you have any online ref. that discusses it?). the actual inlining of code is determined by the compiler, which interprets the inline keyword as a recommendation. Also, according to http://en.wikipedia.org/wiki/Inline_function: "Mainstream C++ compilers [..] support [..] automatically inline [..] even those not marked as inline functions."
Default
@Michael i think you are best off asking this as a separate SO question, linking to these comment boxes. I can't and won't explain that in these comment boxes, it's a PITA (i already tried to explain it, but you seem to be stuck on some other bad explanation that had the same misunderstanding of these things).
Johannes Schaub - litb
@Johannes: I think you're right. :) here it is: http://stackoverflow.com/questions/3647053/what-is-are-the-purposes-of-inline
Default
+1  A: 

typedefs are used for type aliases but can't be used as call alias.

functions (such as suggested as by DeadMG) can be used as a call "alias".

PS. As this is C++ you have lots of options, function pointers, std::tr1::function<> operator overloading and the preprocessor. But in this case it certainly looks like a simple function would be the simplest and best solution.

FuleSnabel
A: 

Look up function pointers.

You can create a function pointer, and assign it to your long function. You can then call this function pointer just like a regular function, wherever your variable is defined.

Function pointers can be confusing, but are used a lot in API callbacks (i.e. you pass a function as an argument to the API, and the API will call that function when something happens (think WndProc)).

Good luck.

Alexander Rafferty
The function pointer idea probably won't work, because as it looks `someFunction` is a nonstatic member function. I already thought of maybe recommending function references, when I realized that's a trap.
Johannes Schaub - litb
A: 

you can do this #define someFunctionAlias MYCLASS::GetInstance()->someFunction()

aeh
Why Oh why Oh why.
Martin York
@Martin well I downvoted this madness. Let's jump in!
Johannes Schaub - litb
I agree in c++ a macro is denied justifiably. There are certain cases where its usage can be justified and i feel this is one. There are no types involved here OP just wants an alias for code readability. The following links shows wherein the SO community has accepted it http://stackoverflow.com/questions/96196/when-are-c-macros-beneficial, http://stackoverflow.com/questions/546997/use-ifdefs-and-define-to-optionally-turn-a-function-call-into-a-comment
aeh