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

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

2
  • 1
    we used to do it because it made cut/paste and searching between the definition/declaration easier. But with modern IDEs there is no need and it's now illegal. Commented May 9, 2011 at 21:26
  • @Martin Becket: Strictly speaking, it was already illegal even in C++98. Even if there was a time when it was legal, it must have been some pre-standard period. Commented May 9, 2011 at 23:51

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

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

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

3 Comments

But not when declaring it inside the class definition, as your example seems to imply.
What do you mean (pehaps you refer to an erroneous post, which I edited shortly after posting, actually copy-and-pasted from the question and forgot to remove the error).
Yes, I referred to the earlier version. Looks fine now.
2

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.

2 Comments

I know it's illegal, but I'm failing to find why. Is there a note somewhere, or is it in the grammar?
@GMan: It is in 8.3/1. Basically, qualified names can only be used to refer to previously declared entities, but not to introduce new entities. See also this defect: open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#482. The old (defective) wording was more explicit, but with regard to this example nothing really changed in the new wording.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.