#cpp #ооп #классы
Почему для return type надо уточнить какой enum TestClass::ePower TestClass::GetPower() , а для входного параметра нет? void TestClass::SetPower( ePower _power ) при написании реализации вне класса ? Весь hello world код: #includeusing namespace std; ////////////////////////////////////////////////////////////////////////// enum class ePower { other, }; ////////////////////////////////////////////////////////////////////////// class TestClass { public: enum class ePower { on, off }; private: ePower power; public: void SetPower( ePower _power ); ePower GetPower(); }; //////////////////////////////////////////////////////////////////// void TestClass::SetPower( ePower _power ) { power = _power; } TestClass::ePower TestClass::GetPower() { return power; } int main() { TestClass a; a.SetPower( TestClass::ePower::on ); }
Ответы
Ответ 1
В соответствии с правилами поиска неквалифицированных имен, в частности, имена, используемые вне декларатора функции-члена класса, как, например, используемые в описании возвращаемого значения, ищутся в той области, где определен класс. Имена, используемые в частности в деклараторе функции-члене класса, ищутся в области определения класса. Поиск неквалифицированных имен для вашего примера описан в параграфах №7 и №8 раздела 3.4.1 Unqualified name lookup стандарта C++. 7 A name used in the definition of a class X outside of a member function body, default argument, exceptionspecification, brace-or-equal-initializer of a non-static data member, or nested class definition29 shall be declared in one of the following ways: — before its use in class X or be a member of a base class of X (10.2), or — if X is a nested class of class Y (9.7), before the definition of X in Y, or shall be a member of a base class of Y (this lookup applies in turn to Y ’s enclosing classes, starting with the innermost enclosing class),30 or — if X is a local class (9.8) or is a nested class of a local class, before the definition of class X in a block enclosing the definition of class X, or — if X is a member of namespace N, or is a nested class of a class that is a member of N, or is a local class or a nested class within a local class of a function that is a member of N, before the definition of class X in namespace N or in one of N ’s enclosing namespaces. и 8 For the members of a class X, a name used in a member function body, in a default argument, in an exceptionspecification, in the brace-or-equal-initializer of a non-static data member (9.2), or in the definition of a class member outside of the definition of X, following the member’s declarator-id31, shall be declared in one of the following ways: — before its use in the block in which it is used or in an enclosing block (6.3), or — shall be a member of class X or be a member of a base class of X (10.2), or — if X is a nested class of class Y (9.7), shall be a member of Y, or shall be a member of a base class of Y (this lookup applies in turn to Y’s enclosing classes, starting with the innermost enclosing class),32 or — if X is a local class (9.8) or is a nested class of a local class, before the definition of class X in a block enclosing the definition of class X, or — if X is a member of namespace N, or is a nested class of a class that is a member of N, or is a local class or a nested class within a local class of a function that is a member of N, before the use of the name, in namespace N or in one of N ’s enclosing namespaces. И сноска 31 31) That is, an unqualified name that occurs, for instance, in a type in the parameter-declaration-clause or in the exception specification. Эти же правила дублируются и в других разделах, где описание относится к классам.
Комментариев нет:
Отправить комментарий