Is it possible to avoid repeating the class name in the implementation file?

I'm guessing this is to avoid lots of "unnecessary typing". Sadly there's no way to get rid of the scope (as many other answers have told you) however what I do personally is get the class defined with all my function prototypes in nice rows, then copy/paste into the implementation file then ctrl-c your ClassName:: on the clip board and run up the line with ctrl-v.


If you want to avoid typing the "Graph::" in front of the printGraph, addEdge etc., then the answer is "no", unfortunately. The "partial class" feature similar to C# is not accessible in C++ and the name of any class (like "Graph") is not a namespace, it's a scope.


No there's not. Not directly at least. You could go for preprocessor tricks, but don't do it.

#define IMPL Graph::

IMPL Graph(int n){}
void IMPL printGraph(){}
void IMPL addEdge(){}
void IMPL removeEdge(){}

Also, you shouldn't even want to do it. What's the point. Besides it being a C++ rule, it lets you know you're actually implementing a member function.


One option is using. If you have method definitions which are in a cpp file that never gets #included, then using is safe (doesn't affect other files):

foo.h:

class FooLongNameSpecialisationsParamaters
{
    int x_;

public:

    int Get () const;
    void Set (int);
};

foo.cpp:

#include "foo.h"

using Foo = FooLongNameSpecialisationsParamaters;

int Foo::Get () const
{
    return x_;
}

void Foo::Set (int x)
{
    x_ = x;
}

main.cpp:

#include "foo.h"

int main ()
{
    //Foo foo; <-- error
    FooLongNameSpecialisationsParamaters foo;

    return 0;
}

No, there is no way to avoid it. Otherwise, how would you know if a given function definition is for a class function or for a static function?