C++ Function Alias
-
Hi, I´m searching for the best way, to define some kind of as function alias in C++ I have the following class:
class c1
{
private:
CString FunctionWithADisturbingLongName(int i);
}And i want the function to have the long function name, because the function of the function has to be explained in it :). But I want to have a shortcut to the function. I want to do this
c1 test;
test.short_1(1);instead of
c1 test;
test.FunctionWithADisturbingLongName(1);Of course I could declare and define short_1 as a member of c1, and then call FunctionWithADisturbingLongName in it, but there has to be a better solution. Thank you for helping!
-
Hi, I´m searching for the best way, to define some kind of as function alias in C++ I have the following class:
class c1
{
private:
CString FunctionWithADisturbingLongName(int i);
}And i want the function to have the long function name, because the function of the function has to be explained in it :). But I want to have a shortcut to the function. I want to do this
c1 test;
test.short_1(1);instead of
c1 test;
test.FunctionWithADisturbingLongName(1);Of course I could declare and define short_1 as a member of c1, and then call FunctionWithADisturbingLongName in it, but there has to be a better solution. Thank you for helping!
#define FunctionWithADisturbingLongName test
That might do the trick as well. :)
Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]
-
#define FunctionWithADisturbingLongName test
That might do the trick as well. :)
Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]
That would probably work... although this might make debugging a bit of a pain.
-
Hi, I´m searching for the best way, to define some kind of as function alias in C++ I have the following class:
class c1
{
private:
CString FunctionWithADisturbingLongName(int i);
}And i want the function to have the long function name, because the function of the function has to be explained in it :). But I want to have a shortcut to the function. I want to do this
c1 test;
test.short_1(1);instead of
c1 test;
test.FunctionWithADisturbingLongName(1);Of course I could declare and define short_1 as a member of c1, and then call FunctionWithADisturbingLongName in it, but there has to be a better solution. Thank you for helping!
The obvious way would be:
class cl
{
public: //or private, but with your example above,
// the external interface for a class should be public
CString short_l(int i){ return FunctionWithADisturbingLongName(i);}
}Now, this has the added clarity of still allowing the compiler to point you in the correct direction for debugging errors. Using a macro would probably switch names on you all the time (if there's an error it'll report the substituted string).
-
That would probably work... although this might make debugging a bit of a pain.
I could sense the tears falling as I typed in that answer. :)
Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]
-
The obvious way would be:
class cl
{
public: //or private, but with your example above,
// the external interface for a class should be public
CString short_l(int i){ return FunctionWithADisturbingLongName(i);}
}Now, this has the added clarity of still allowing the compiler to point you in the correct direction for debugging errors. Using a macro would probably switch names on you all the time (if there's an error it'll report the substituted string).
This is probably the safest and most sensible way of accomplishing what the OP has requested. :)
Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]
-
I could sense the tears falling as I typed in that answer. :)
Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]
:laugh: ...I still five'd the answer since it would probably work.
-
Hi, I´m searching for the best way, to define some kind of as function alias in C++ I have the following class:
class c1
{
private:
CString FunctionWithADisturbingLongName(int i);
}And i want the function to have the long function name, because the function of the function has to be explained in it :). But I want to have a shortcut to the function. I want to do this
c1 test;
test.short_1(1);instead of
c1 test;
test.FunctionWithADisturbingLongName(1);Of course I could declare and define short_1 as a member of c1, and then call FunctionWithADisturbingLongName in it, but there has to be a better solution. Thank you for helping!
-
Hi, I´m searching for the best way, to define some kind of as function alias in C++ I have the following class:
class c1
{
private:
CString FunctionWithADisturbingLongName(int i);
}And i want the function to have the long function name, because the function of the function has to be explained in it :). But I want to have a shortcut to the function. I want to do this
c1 test;
test.short_1(1);instead of
c1 test;
test.FunctionWithADisturbingLongName(1);Of course I could declare and define short_1 as a member of c1, and then call FunctionWithADisturbingLongName in it, but there has to be a better solution. Thank you for helping!
Me think that it is a bad idea. Your code will be peppered with function calls to short and/or abbreviated method names that will not reflect what it actually do; and the user (other developers) will need to look at the documentation to know what it should be doing.
Watched code never compiles.
-
:laugh: ...I still five'd the answer since it would probably work.
It will work, Windows uses it all over the place for the ASCII/Unicode versions of function names.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
The obvious way would be:
class cl
{
public: //or private, but with your example above,
// the external interface for a class should be public
CString short_l(int i){ return FunctionWithADisturbingLongName(i);}
}Now, this has the added clarity of still allowing the compiler to point you in the correct direction for debugging errors. Using a macro would probably switch names on you all the time (if there's an error it'll report the substituted string).
Stick an __inline in there and that ought to do it.
class cl
{
public: //or private, but with your example above,
// the external interface for a class should be public
__inline CString short_l(int i){ return FunctionWithADisturbingLongName(i);}
} -
Me think that it is a bad idea. Your code will be peppered with function calls to short and/or abbreviated method names that will not reflect what it actually do; and the user (other developers) will need to look at the documentation to know what it should be doing.
Watched code never compiles.
oh, I agree that this would be bad practice. Basically, you have a descriptive function name that makes the code reading for understanding far easier. But instead, you'll have code that uses short, cryptic names because it's easier to type (unless you're letting the wizard auto-complete function names) There's a reason that .NET Obfuscation routines create names like "N1", "N2", etc. It's because it makes the code harder to read and understand. Why do that on purpose to code you have to maintain.
-
It will work, Windows uses it all over the place for the ASCII/Unicode versions of function names.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
I wasn't doubting it. :)
-
oh, I agree that this would be bad practice. Basically, you have a descriptive function name that makes the code reading for understanding far easier. But instead, you'll have code that uses short, cryptic names because it's easier to type (unless you're letting the wizard auto-complete function names) There's a reason that .NET Obfuscation routines create names like "N1", "N2", etc. It's because it makes the code harder to read and understand. Why do that on purpose to code you have to maintain.
Agree too... unless the short names still provide sufficient information to make it readable, although if that's the case... why keep the long names at all (unless it's a library of sorts)...
-
Me think that it is a bad idea. Your code will be peppered with function calls to short and/or abbreviated method names that will not reflect what it actually do; and the user (other developers) will need to look at the documentation to know what it should be doing.
Watched code never compiles.
-
I wasn't doubting it. :)
The word probably implied that you were not sure. :~
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
Hi, I´m searching for the best way, to define some kind of as function alias in C++ I have the following class:
class c1
{
private:
CString FunctionWithADisturbingLongName(int i);
}And i want the function to have the long function name, because the function of the function has to be explained in it :). But I want to have a shortcut to the function. I want to do this
c1 test;
test.short_1(1);instead of
c1 test;
test.FunctionWithADisturbingLongName(1);Of course I could declare and define short_1 as a member of c1, and then call FunctionWithADisturbingLongName in it, but there has to be a better solution. Thank you for helping!
There are several ways 1. #define works, if you choose a reasonably unique name, but it clutters the global namespace and therefore not a good idea. 2. declaring a second function that wraps the first adds code cluttering in your original class 3. a function pointer could work, but involves awkward syntax both for defining the pointer and calling the function 4. a function object would add quite a lot of code, but requires neither changing your original class nor dereferencing to invoke the function:
class shortname {
public:
shortname(class c1* p) : pclass(p) {}
CString operator()(int i) { return pclass->FunctionWithADisturbingLongName(i); }
private:
class c1* pclass;
};void foo() {
c1 test;
shortname(&test);
CString result = shortname(1);
}