使用 const 限定符获取对象的私有属性时出现问题

发布于 2024-09-29 03:30:05 字数 1485 浏览 0 评论 0原文

我是 C++ 的新手,我遇到了一个非常愚蠢的问题。

我有一个 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;
}

当我编译它时,我收到以下错误:

calsaverini@Ankhesenamun:~/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 不太了解,就此而言......),但我需要弄乱那些了解 C++ 的人编写的代码。 :(

关于此复制构造函数的任何线索或评论也将非常感激。:P

I'm a completely new to C++ and I'm having a very stupid problem.

I have a Graph class and I need to create a copy constructor for it. This is my class:

#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;
};

There's a function int igraph_copy(igraph_t * to, const igraph_t * from) in igraph.h that copies an igraph_t type adequately.

The constructor and destructor are trivial and work normally, and I have the following copy constructor:

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;
}

When I compile this, I got the following errors:

calsaverini@Ankhesenamun:~/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

I feel this must be something very basic I didn't get about what the const qualifier means.

I don't really know C++ (and I really don't know C that much deeply, for that matter...) but I need to mess with code made by people who do. :(

Any clues or remarks about this copy constructor will be also very humbly appreciated. :P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人间不值得 2024-10-06 03:30:05

getGraph 函数需要使用 const 限定符进行声明:

const igraph_t* getGraph() const { ... }

这是因为 >other 是一个常量引用。当对象或引用是常量时,您只能调用使用 const 限定符声明的该对象的成员函数。 (const 出现在函数名称和参数列表之后。)

请注意,这还要求您返回一个常量指针。

在 C++ 中,编写两个“get”函数很常见,一个是常量,另一个是非常量,以便处理这两种情况。因此,您可以声明两个 getGraph() 函数:

const igraph_t* getGraph() const { ... }

...和

​​igraph_t* getGraph() { ... }

如果对象是常量,则将调用第一个,如果对象是非常量,则将调用第二个。您可能应该阅读有关 const 成员函数限定符< /a>,以及 const- Correctness 一般而言。

The getGraph function needs to be declared with the const qualifier:

const igraph_t* getGraph() const { ... }

This is because other is a constant reference. When an object or reference is constant, you can only call member functions of that object which are declared with the const qualifier. (The const which appears after the function name and parameter list.)

Note that this also requires you to return a constant pointer.

It's common in C++ to write two "get" functions, one which is constant and the other non-constant, in order to deal with both cases. So you could declare two getGraph() functions:

const igraph_t* getGraph() const { ... }

...and

igraph_t* getGraph() { ... }

The first will be called if the object is constant, and the second will be called if the object is non-constant. You should probably read more about the const member-function qualifier, as well as const-correctness in general.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文