Reputation: 8870
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.
Upvotes: 3
Views: 1621
Reputation: 385144
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.
Upvotes: 2
Reputation: 7625
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.
Upvotes: -1