0

Is there a reason we can not write in C++

class MyClass
{
public:
    void MyClass::MyMethod();  // <----
}

it gives 'extra qualification' or some such compile error but is there a reason for that or it's just so happened?

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158

2 Answers2

6

Because that's the syntax for declarations; they declare an unqualified name to have a particular meaning within the scope of the declaration.

If a scope qualification were allowed, it would always be either redundant (specifying the current scope) or wrong (since you can't declare something in another scope).

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
4

The reason is that it is illegal syntax. Besides, why would you need to add a redundant extra qualification?

The member function is being declared in the context of the class definition, explicitly qualifying it as a member function of that class is unnecessary - and it compromises readability.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451