Question

I have files MyClass.hpp and MyClass.cpp

MyClass.hpp

class MyClass {
public:
    void method1();
    void method2();
};

MyClass.cpp

#include "MyClass.hpp"
void MyClass::method1() {
}

void MyClass::method2() {
}

I find it a little silly that I have to write out the MyClass:: every time I have to write a method implementation. Is there some sort of syntactic sugar that lets me just group all my implementation together?

Perhaps something like

namespace MyClass {
    void method1() {
    }

    void method2() {
    }
}

I don't mind using C++11 features, but I would like to stick with portable solutions.

EDIT:

I realize that the code above as written wouldn't work. I was just using it as an illustration of how I imagine some syntactic sugar would work to make things more convenient.

Was it helpful?

Solution

No, it is not possible.

The only way is to define your member functions directly inside the class definition, which makes them inline and may or may not be what you want.

Personally, I reckon having function definitions in source files is well worth having to type out a class name once in each.

OTHER TIPS

No, there is no other-way. You have to repeat MyClass::

It is not possible to identify a method using namespace. In a namespace there can be several classes, non-member methods .... Since a class method can be defined in any source file (other than the header declaring the class itself), you have to specify which class a method belongs to while defining it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top