C++ - Multiple names for one function?

Started by
10 comments, last by Zahlman 17 years, 11 months ago
Hi all, I'm asking this out of interest rather then because it's a feature I absolutly need, but whats the best way to give a function multiple names in C++? Let's say I have a vector class with three functions - getX(), getY(), and getZ(). Now many people use a different convention and like to call the vector component i,j, and k so i'd also like to supply getI(), getJ(), and getK() functions which do exactly the same as thier x,y,z counterparts. Whats the best way to achieve this? I see several options: 1) Copy and paste the code and rename functions. 2) Have getI() call getX() internally. 3) Clever tricks with functions pointers/references?! 4) Clever tricks with templates? Although not exactly critical, I do believe this would be a useful language feature. Maybe some languages provide it already? Any thoughts? David
Advertisement
With a function like getI(), you're not going to save much code by calling getX() instead of copy-pasting return x. For functions that do have some code in them, calling getX() is the easiest way to go (and inlining should remove its cost).

However, I think adding tons of identical functions for accessing xyz/ijk/rgb just pollutes the class and makes code harder to understand. I'd just stick with xyz. If you don't like using xyz for something like rgb color values, you should create a class for that purpose.
Quote:Original post by esuvs
Hi all, I'm asking this out of interest rather then because it's a feature I absolutly need, but whats the best way to give a function multiple names in C++?

Let's say I have a vector class with three functions - getX(), getY(), and getZ(). Now many people use a different convention and like to call the vector component i,j, and k so i'd also like to supply getI(), getJ(), and getK() functions which do exactly the same as thier x,y,z counterparts. Whats the best way to achieve this? I see several options:

1) Copy and paste the code and rename functions.
2) Have getI() call getX() internally.
3) Clever tricks with functions pointers/references?!
4) Clever tricks with templates?

Although not exactly critical, I do believe this would be a useful language feature. Maybe some languages provide it already?



The point of encapsulation is that private date members don't need to be accessed directly (by their names), rather you simply provide accessor methods. So, if you really care what these members are named internally, as well as their accessors, you could simply provide a wrapper for such a class (as already suggested).
Alternatively, you could also provide each number with its own wrapper, i.e. x.set(), x.get() etc-that way, you could have everything template-based.
Likewise, you could simply map custom getters/setters to the class instance's methods, so that you can use custom names.

Quote:Effective C++ 2nd Edition by Scott Meyers
A large interface can also lead to confusion. Suppose you create a class that supports cognition for an artificial intelligence application. One of your member functions is called think, but you later discover that some people want the function to be called ponder, and others prefer the name ruminate. In an effort to be accommodating, you offer all three functions, even though they do the same thing. Consider then the plight of a potential client of your class who is trying to figure things out. The client is faced with three different functions, all of which are supposed to do the same thing. Can that really be true? Isn't there some subtle difference between the three, possibly in efficiency or generality or reliability? If not, why are there three different functions? Rather than appreciating your flexibility, such a potential client is likely to wonder what on earth you were thinking (or pondering, or ruminating over).

Σnigma
Quote:Original post by Enigma
Quote:Effective C++ 2nd Edition by Scott Meyers
A large interface can also lead to confusion. Suppose you create a class that supports cognition for an artificial intelligence application. One of your member functions is called think, but you later discover that some people want the function to be called ponder, and others prefer the name ruminate. In an effort to be accommodating, you offer all three functions, even though they do the same thing. Consider then the plight of a potential client of your class who is trying to figure things out. The client is faced with three different functions, all of which are supposed to do the same thing. Can that really be true? Isn't there some subtle difference between the three, possibly in efficiency or generality or reliability? If not, why are there three different functions? Rather than appreciating your flexibility, such a potential client is likely to wonder what on earth you were thinking (or pondering, or ruminating over).

Σnigma


hihi, I was just about to post almost the exact same lines from Meyers. :D
Basically, you seem to be looking for some sort of advanced rvalue support in template instantiations, i.e.:

//------PSEUDO CODE------------template <class TYPE, class NAME>class num {public: TYPE get() {return NAME;} void set(TYPE t) {NAME=t;}//override operator=private: TYPE NAME;};template <class XTYPE, class XNAME, class YTYPE, class YNAME, class ZTYPE, class ZNAME>class vector {public: num<XTYPE,XNAME> XNAME; num<YTYPE,YNAME> YNAME; num<ZTYPE,ZNAME> ZNAME;};//of course, if such support was available one could directly do something along the lines of:template <class XTYPE, class XNAME, class YTYPE, class YNAME, class ZTYPE, class ZNAME>class Vector {//dynamically named getters/setters, based on template params:public:XTYPE get{XNAME} () {return XNAME;}void set{XNAME} (XTYPE x) {XNAME=x;}XTYPE get{YNAME} () {return YNAME;}void set{YNAME} (YTYPE x) {YNAME=x;}XTYPE get{ZNAME} () {return ZNAME;}void set{ZNAME} (ZTYPE x) {ZNAME=x;}private: XTYPE XNAME; YTYPE YNAME; ZTYPE ZNAME;};int main(){vector < double, x, double, y, double, z> MyVec;MyVec.x.set(10.00);MyVec.y.set(12.00);MyVec.z.set(24.00);vector < double, X, double, Y, double, Z> MyVec2;MyVec2.setX(10.00);MyVec2.setY(12.00);MyVec2.setZ(24.00);return 0;}


The alternative would be to allow template parameters to modify the naming of methods, too
Well, I guess I can't really argue with Meyes :-D Guess I'll just steer clear of it, thanks for the input guys!
Use a define.
#define getI getX
Insufficent Information: we need more infromationhttp://staff.samods.org/aiursrage2k/
Quote:Original post by Anonymous Poster
Basically, you seem to be looking for some sort of advanced rvalue support in template instantiations, i.e.:

*** Source Snippet Removed ***

The alternative would be to allow template parameters to modify the naming of methods, too


That won't work, because you can pass only types to your template-vector and NOT names.


If you would really make such a complicated interface for a class that needs the best performance I would not use it. Everyone who understands LA won't have a problem by using other names.
You do it all the time when using other libraries, reading articles or books or something else, why should other be any different?

Quote:Original post by Anonymous Poster
Basically, you seem to be looking for some sort of advanced rvalue support in template instantiations, i.e.:

*** Source Snippet Removed ***

The alternative would be to allow template parameters to modify the naming of methods, too


That won't work, because you can pass only types to your template-vector and NOT names.


If you would really make such a complicated interface for a class that needs the best performance I would not use it. Everyone who understands LA won't have a problem by using other names.
You do it all the time when using other libraries, reading articles or books or something else, why should other be any different?

This topic is closed to new replies.

Advertisement