简体   繁体   中英

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. What confused me is the two member functions ElapsedTime()? Is this allowed for the two functions to have the same signatures such as function name and parameters type?

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. Constness is part of member function signature.

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. 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 . In the other method (without the const), the type of "this" is just const pointer .

Hence, you have two methods with different signature, and there is no problem at all.

The signatures are different because one has const qualifier. http://www.cprogramming.com/tutorial/const_correctness.html

Is this allowed

This usage is allowed as over.load#2.2 states:

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]). 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. 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. 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 . [ 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& . Moroever, there is no static member function declaration for ElapsedTime with the same paremeer-type-list. 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 . 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* .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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