Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why specify class name when declaring member functions?

Tags:

c++

class

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

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

like image 610
Oliver Zheng Avatar asked Jan 29 '26 23:01

Oliver Zheng


2 Answers

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

like image 121
idz Avatar answered Feb 01 '26 11:02

idz


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

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

void C::Test() { ... }
like image 42
Christian Rau Avatar answered Feb 01 '26 13:02

Christian Rau