Oliver Zheng
Oliver Zheng

Reputation: 8199

Why specify class name when declaring member functions?

class C : public B
{
public:
    void C::Test();
};

What is the point of specifying C in the declaration of the member function?

Upvotes: 2

Views: 166

Answers (4)

AnT stands with Russia
AnT stands with Russia

Reputation: 320719

Not only there's no point, it is downright illegal (see 8.3/1 in the language standard). In general in C++ language qualified names are allowed only when you are referring to a previously declared entity, but not when you are introducing a new entity (there are some exceptions from this rule, but none of them apply here).

The code you posted would require a diagnostic message from any conforming compiler, since your member function declaration is invalid.

Upvotes: 2

Christian Rau
Christian Rau

Reputation: 45968

This is only neccessary when defining the method outside of the class:

class C : public B
{
public:
    void Test();
};

void C::Test() { ... }

Upvotes: 2

idz
idz

Reputation: 12988

You shouldn't do this. Many modern compilers will treat this as a syntax error, for example, g++ 4.2.1 will!

Upvotes: 4

AVH
AVH

Reputation: 11516

There is no point, no need to do this. Since the declaration of Test is inside the scope of the declaration of C, the compiler knows that the function Test is a member of C.

Upvotes: 1

Related Questions