简体   繁体   English

在同一个类/结构中具有相同签名的相同 function? 超载?

[英]Same function with same signatures in the same class/struct? Overload?

I have seen a code portion in the boost example which is used to construct a state machine.我在 boost 示例中看到了用于构建 state 机器的代码部分。 What confused me is the two member functions ElapsedTime()?让我困惑的是这两个成员函数ElapsedTime()? Is this allowed for the two functions to have the same signatures such as function name and parameters type?这是否允许两个函数具有相同的签名,例如 function 名称和参数类型?

I have googled a lot but without any luck to find relevant info about this.我用谷歌搜索了很多但没有找到相关信息。 Any advice on this would be greatly appreciated.对此的任何建议将不胜感激。

struct Active : sc::simple_state< Active, StopWatch, Stopped >
{
public:
    typedef sc::transition< EvReset, Active > reactions;

    Active() : elapsedTime_( 0.0 ) {}
    double ElapsedTime() const { return elapsedTime_; }
    double & ElapsedTime() { return elapsedTime_; }

private: 
    double elapsedTime_;
};

They do not have the same signatures - one is const and another one is not. 它们没有相同的签名 - 一个是const而另一个不是。 Constness is part of member function signature. Constness是成员函数签名的一部分。

Signature of a function is defined by the name and by the argument types. 函数的签名由名称和参数类型定义。 You have two functions with the same name, but they do not get the same arguments! 你有两个具有相同名称的函数,但它们没有相同的参数!

You might wonder how could it be? 你可能想知道它怎么可能?

So, each member function gets another parameter implicitly: this is the "this" pointer. 因此,每个成员函数都隐式获取另一个参数:这是“this”指针。 A pointer to the object who called that method. 指向调用该方法的对象的指针。

When you add const in the end of the method, you specify the "this" argument as a const pointer to const . 在方法的末尾添加const时,将“this”参数指定为const的const指针 In the other method (without the const), the type of "this" is just const pointer . 在另一个方法(没有const)中,“this”的类型只是const指针

Hence, you have two methods with different signature, and there is no problem at all. 因此,您有两种具有不同签名的方法,并且根本没有问题。

The signatures are different because one has const qualifier. 签名是不同的,因为一个具有const限定符。 http://www.cprogramming.com/tutorial/const_correctness.html http://www.cprogramming.com/tutorial/const_correctness.html

Is this allowed这是允许的吗

This usage is allowed as over.load#2.2 states: over.load#2.2状态允许这种用法:

Member function declarations with the same name and the same parameter-type-list cannot be overloaded if any of them is a static member function declaration ([class.static]).如果其中任何一个是 function 成员 function 声明 ([class.static]),则不能重载具有相同名称和相同参数类型列表的成员 function 声明。 Likewise, member function template declarations with the same name, the same parameter-type-list, and the same template parameter lists cannot be overloaded if any of them is a static member function template declaration.同样,具有相同名称、相同参数类型列表和相同模板参数列表的成员 function 模板声明不能重载,如果它们中的任何一个是 static 成员 function 模板声明。 The types of the implicit object parameters constructed for the member functions for the purpose of overload resolution ([over.match.funcs]) are not considered when comparing parameter-type-lists for enforcement of this rule.为重载决策 ([over.match.funcs]) 的目的为成员函数构造的隐式 object 参数的类型在比较参数类型列表以执行此规则时不被考虑。 In contrast, if there is no static member function declaration among a set of member function declarations with the same name and the same parameter-type-list, then these member function declarations can be overloaded if they differ in the type of their implicit object parameter .相反,如果在一组具有相同名称和相同参数类型列表的成员 function 声明中没有成员 static 成员 function 声明,则如果这些成员 function 声明在其隐式 object 参数的类型不同,则可以重载. [ Example: The following illustrates this distinction: [示例:以下说明了这种区别:

(end quote) (结束引用)

Now, in your example the first overload has a const qualifer which means that its implicit object parameter has type const Active& while the second overload has no const qualifer meaning that its implicit object parameter is of type Active& .现在,在您的示例中,第一个重载具有const限定符,这意味着其隐式 object 参数的类型为const Active&而第二个重载没有const限定符,这意味着其隐式 object 参数的类型为Active& Moroever, there is no static member function declaration for ElapsedTime with the same paremeer-type-list.此外,没有 static 成员 function 声明具有相同的ElapsedTime -type-list 的 ElapsedTime。 Hence using the above quoted statement, this means that the given usage is allowed.因此,使用上面引用的语句,这意味着允许给定的用法。

Note笔记

Note that the currently accepted answer is technically incorrect because it claims that this is a const pointer to const .请注意,当前接受的答案在技术上是不正确的,因为它声称this指向 const 的 const 指针 But in reality the standard specifies that inside a const qualified member function the this pointer is of type const X* while inside a non-const qualified member function the this pointer is of type X* .但实际上,标准规定const限定成员 function 中, this指针的类型为const X*而在非 const 限定成员 function 中, this指针的类型为X*

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

相关问题 如何返回与函数在同一类中声明的结构? - How to return struct declared in the same class with function? 重载相同操作的友元函数和类方法的优先级 - Priority of friend function and class method that overload the same operation 为什么这些功能签名不一样? - Why aren't these function signatures treated the same? 将类构造函数重载置于同一个类的另一个重载中 - Putting a class constructor overload INSIDE another overload of the same class 类成员函数无法访问同一类的私有结构节点? - Class member function unable to access a private struct node of the same class? 创建具有相同名称的结构和类 - Creating a struct and a class with the same name 相同函数的模板化和非模板化版本是否被视为过载? - Is a templated and a nontemplated version of the same function considered an overload? C ++中具有相同参数类型的函数重载 - Function overload with same parameter type in C++ 为什么我不能将此运算符重载在与结构相同的命名空间中? - Why I cannot put this operator overload in the same namespace as the struct? 如何将一个类的函数添加到同一个类的另一个函数内的重载结果列表中? - How to add a class' function to the overload resoution list within the other function of the same class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM