Is it possible to avoid repeating the class name i

2020-01-25 11:02发布

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(){}

标签: c++ class syntax
7条回答
贪生不怕死
2楼-- · 2020-01-25 11:46

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.

查看更多
登录 后发表回答