简体   繁体   English

c ++类函数别名

[英]c++ class function aliases

I was wondering if there was a simple way to write an alias of a c++ class function. 我想知道是否有一种简单的方法来编写c ++类函数的别名。 For instance, if I have some list container object, a logical function would be 例如,如果我有一些list容器对象,那么逻辑函数就是

    int list::length() { return len; }

But another logical alias that programmers might use could be 但程序员可能使用的另一个逻辑别名可能是

    int list::size() { return len; }

So, instead of writing both functions with their full body, is there any way to make list::size() an alias of list::length() such that it isn't a duplicate when compiled, but rather references the same function? 因此,有没有什么方法可以使list::size()成为list::length()的别名,这样在编译时它不是重复的,而是引用相同的函数?

I've read that you can do this with #define , but I don't want to cause any confusion with other code-names somewhere totally out of scope (ie a 'size' variable). 我已经读过你可以用#define做到这一点,但是我不想在某个地方完全超出范围(即'size'变量)引起任何混淆。
I've also read that function pointers can fix it, but that isn't exactly an alias (since it has to apply de-referencing), nor can function pointers be given a declaration, giving it a confusing help-line to users (I would think), plus the confusion if ever I need to nest my code inside another object (I have to adjust the scope). 我还读过函数指针可以修复它,但这不是一个别名(因为它必须应用去引用),也不能给函数指针一个声明,给用户一个令人困惑的帮助线(我想),加上混乱,如果我需要将我的代码嵌套在另一个对象中(我必须调整范围)。

One of my guesses is, will the following be taken as a direct function alias by most optimizing compilers: 我的一个猜测是,大多数优化编译器将以下内容作为直接函数别名:

    inline int list::length() { return len; }
    inline int list::size() { return length(); }

Or, is there any strict 'alias' syntax for c++? 或者,c ++有任何严格的“别名”语法吗? (I couldn't find any - wasn't sure) (我找不到 - 不确定)
So then, what would be the most efficient way of doing this? 那么,最有效的方法是什么呢?

EDIT: I've accepted the answer simply to wrap up the question, since it's only a curiosity of mine. 编辑:我接受了答案只是为了结束这个问题,因为这只是我的一个好奇心。 Anyone with good information, please add comments or answer, and I may even change my answer. 任何有良好信息的人,请添加评论或回答,我甚至可能会改变我的答案。

I would not use the preprocessor and #define to do this. 我不会使用预处理器和#define来执行此操作。 In general preprocessor should be a last resort in C++. 通常,预处理器应该是C ++中的最后手段。 See this C++ FAQ on inline functions which also contains a section on the various evils of using macros of the preprocessor. 请参阅关于内联函数的C ++ FAQ,其中还包含有关使用预处理器宏的各种恶意的部分。

The approach I would use would be to have a function that will have several different aliases with a more complicated function and interface you would do something like the following: 我将使用的方法是拥有一个函数,它将具有几个不同的别名,具有更复杂的函数和接口,您可以执行以下操作:

int list::length(string xString, int iValue) {
  int iReturnValue = 0;  // init the return value
  //  do stuff with xString and iValue and other things
  return iReturnValue;
}

Then do something like the following for an alias. 然后为别名执行类似下面的操作。

inline int list::size(string xString, int iValue) {return length(xString, iValue);}

The inline should basically just replace the alias with the actual function call. 内联应该基本上只是用实际的函数调用替换别名。

See also this stack overflow posting Giving a function implementation more than one name . 另请参见此堆栈溢出过程为函数实现提供多个名称 It provides some reasons why you might not want to do this. 它提供了您可能不想这样做的一些原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM