我是一个全新的C++,我有一个非常愚蠢的问题。const修饰符获得对象的私有属性的问题
我有一个Graph类,我需要为它创建一个拷贝构造函数。这是我的课:
#include <igraph.h>
#include <iostream>
using namespace std;
class Graph {
public:
Graph(int N); // contructor
~Graph(); // destructor
Graph(const Graph& other); // Copy constructor
igraph_t * getGraph();
int getSize();
private:
igraph_t graph;
int size;
};
有在igraph.h
是一个函数int igraph_copy(igraph_t * to, const igraph_t * from)
那份一个igraph_t
型充分。
构造函数和析构函数是微乎其微的,工作正常,我有以下的拷贝构造函数:
Graph :: Graph(const Graph& other) {
igraph_t * otherGraph = other.getGraph();
igraph_copy(&graph, otherGraph);
size = other.getSize();
}
igraph_t * Graph :: getGraph(){
return &graph;
}
int Graph :: getSize() {
return size;
}
当我编译此,我得到了以下错误:
[email protected]:~/authC/teste$ make
g++ -I/usr/include/igraph -L/usr/local/lib -ligraph -c foo.cpp -o foo.o
foo.cpp: In copy constructor ‘Graph::Graph(const Graph&)’:
foo.cpp:30: error: passing ‘const Graph’ as ‘this’ argument of ‘igraph_t* Graph::getGraph()’ discards qualifiers
foo.cpp:32: error: passing ‘const Graph’ as ‘this’ argument of ‘int Graph::getSize()’ discards qualifiers
make: *** [foo.o] Error 1
我觉得这必须是非常基本的东西,我没有得到有关const限定符的含义。
我真的不了解C++(对于这个问题,我真的不太了解C),但是我需要捣乱那些做出来的代码。 :(
这个拷贝构造函数任何线索或言论也会很虚心地赞赏:P。