Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

syntax

class

Is there a way to avoid the Graph:: repetition in the implementation file, yet still split the class into header + implementation? Such as in:

Header File:

#ifndef Graph_H
#define Graph_H

class Graph {
public:
    Graph(int n);
    void printGraph();
    void addEdge();
    void removeEdge();
};

#endif

Implementation File:

Graph::Graph(int n){}
void Graph::printGraph(){}
void Graph::addEdge(){}
void Graph::removeEdge(){}
like image 829
Ofek Ron Avatar asked Jun 04 '12 19:06

Ofek Ron


2 Answers

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.

like image 158
Kneight Avatar answered Nov 06 '22 05:11

Kneight


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.

like image 13
Viktor Latypov Avatar answered Nov 06 '22 04:11

Viktor Latypov