Member functions can be overloaded; but member functions can only overload other member functions of this class. Member functions of a class are not related to ordinary non-member functions and functions declared in other classes, and they cannot be overloaded.
Member functions can be overloaded
Member functions can only overload other member functions of this class. Member functions of a class are not related to ordinary non-member functions and functions declared in other classes, and they cannot be overloaded. The same rules apply to overloaded member functions as to ordinary functions: the number and type of formal parameters of two overloaded members cannot be exactly the same. The function matching process used for calls to non-member overloaded functions also applies to calls to overloaded member functions.
Define overloaded member functions
To illustrate overloading, two overloaded members of the Screen class can be given to return a specific character from the window. Of the two overloaded members, one version returns the character indicated by the current cursor, and the other returns the character at the specified row and column:
class Screen { public: typedef std::string::size_type index; // return character at the cursor or at a given position char get() const { return contents[cursor]; } char get(index ht, index wd) const; // remaining members private: std::string contents; index cursor; index height, width; };
Like any overloaded function, provide the appropriate number and Type of actual parameters to choose which version to run:
Screen myscreen; char ch = myscreen.get();// calls Screen::get() ch = myscreen.get(0,0); // calls Screen::get(index, index)
Recommended tutorial: "c Video Tutorial"
The above is the detailed content of Can member functions be overloaded?. For more information, please follow other related articles on the PHP Chinese website!