Sign in to follow this  
PolyVox

C++ - Multiple names for one function?

Recommended Posts

PolyVox    712
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

Share this post


Link to post
Share on other sites
hh10k    589
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Enigma    1410
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

Share this post


Link to post
Share on other sites
Katachi    131
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
PolyVox    712
Well, I guess I can't really argue with Meyes :-D Guess I'll just steer clear of it, thanks for the input guys!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by Anonymous Poster
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.

Well, Thanks (I guess) for pointing this out but I was fully aware of that (if you check out the "source" you'll see it isn't even valid C++!), guess why the snippet was prepended with "pseudo-code"?

Sorry, if I didn't make clear that this is something that isn't supported by the current C++ standard. Nevertheless, there are theoertically also other things you could do for more generic template use.



Share this post


Link to post
Share on other sites
Zahlman    1682
Personally I think get/set pairs are an abomination anyway, but I will include support for them...


// There are spaces after the backslashes in the macro;
// you should take those out when you use it in code. They are only there
// to accomodate the forum's parser
#define MEMBER(x) private: \
int x; \
public: \
int get##x { return x; } \
void set##x(int x##_) { x = x##_; }

#define VECTOR(name, x, y, z) struct name { \
MEMBER(x) \
MEMBER(y) MEMBER(z) }

VECTOR(colour, r, g, b);

colour c;




EDIT: P.S. I also agree with Enigma; I'm just bored. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this