如何使用指针和引用 c++还有瓦尔格林德?
我有以下代码:
[test.h]
class MyClass
{
public:
string Name;
MyClass();
void method(MyClass &obj);
}
[test.cpp]
void MyClass::method(MyClass &obj)
{
cout<<obj.Name<<endl;
}
[main.cpp]
#include "test.h"
void main()
{
MyClass *class = new MyClass();
class->Name="Foo";
class->method(*class);
delete class;
}
我想问这是否是通过引用发送包含对象的方法的正确方法。 我是否正确地释放了分配的内存? 我问这个问题是因为对于一个类似的例子,当测试 valgrind 时我有这个:条件跳转或移动依赖于统一的值。
我在 Ubuntu 下使用 c++ 工作。我的编译器是g++。 欣赏!! 编辑!!
为什么我不能将 INT VALUE=0;在 test.h 文件中?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
class
是 C++ 中的保留字,不能将其用作变量的名称。此外,除非您明确要求,否则不需要动态分配对象。如果可能的话,自动分配:
class
is a reserved word in C++, you cannot use it as the name of your variables.Moreover, there's no need to allocate the object dynamically unless you explicitly require that. If possible, just allocate it automatically:
如果
VALUE
是类变量的一部分,则不允许将其作为类声明的一部分进行初始化。相反,应该使用构造函数初始值设定项列表。然而,如果
VALUE
被定义为全局变量,那么这也会导致问题。当您在多个源文件中包含test.h
时,会导致多个声明错误。在源文件中定义它,并使用 extern 关键字跨多个翻译单元进行访问。If
VALUE
is part of class variables, then it is not allowed to initialize as a part of class declaration. Instead, constructor initializer list should be used.How ever, if
VALUE
is defined as a global variable then this too leads to problems. When you includetest.h
in multiple source files, it leads to multiple declaration errors. Define it in a source file and access across multiple translation units using the extern key word.编译=> valgrind g++ ./main
并查看是否存在内存泄漏。
确保您已经安装了 valgrind,否则您应该通过 apt-get 或 ubuntu 软件中心安装它。
希望这有帮助..
compile => valgrind g++ ./main
and see if there is any memory leak.
be sure you have already installed valgrind, in other case you should install it by apt-get or ubuntu software center.
hope this helps..
我觉得很奇怪,编译器没有喊出
class
是保留关键字。无论如何,在我看来,使用 method(MyClass &obj) 并不是一个好主意,因为您已经有一个指针,它是取消引用,然后再次通过引用获取。我只是不知道它指向什么,这可能就是出现警告的原因。
尝试以下操作,看看是否仍然有相同的警告,我现在无法测试:
I find it strange the compiler didn't yell that
class
is a reserved keyword.Anyway, using
method(MyClass &obj)
is not a good idea in my opinion, since you already have a pointer, which is dereferences, and then taken by reference again. I just don't know what it is pointing at, and that may be why the warning appears.Try the following and see if it still has the same warnings, I can't test it right now:
看起来没有内存泄漏和未初始化值的使用。
Name
成员将由其默认构造函数初始化。 Valgrind 报告的错误可能来自标准库或您可能使用的其他库。另请注意,此错误并不总是表明代码中存在错误。Looks like there is no memory leaks and use of uninitialized values.
Name
member will be initialized by it's default constructor. The errors reported by Valgrind may come from Standard Library or some other library you maybe used. Also note that this error not always indicate the bug in code.这是正确的。搞砸事情的唯一方法是忘记删除、多次删除,或者 new 和 delete 之间有或没有括号不匹配。但是,有一些事情需要注意:
在您的示例中,可以将参数作为 const ref 传递,因为您不更改它。
相应地也在 cpp 中:
您根本不需要使用 new 在堆上分配对象。事实上,你几乎总是可以这样使用语法:
每当你拥有大量原始数据,特别是经典数组时,它们通常是类成员(即使这是一种罕见的情况,因为像向量这样很棒的 stl 容器),你让该类处理内存管理。这再次允许在堆栈上创建这些类,通过让它们超出范围来进行清理,并且该类将负责将大量数据实际存储在堆上。
因此,虽然您的代码是正确的,但您实际上很少需要担心这一点,因为您
it is correct. The only way to mess things up is by forgetting a delete, deleting more than once, or a mismatch between new and delete with or without brackets. However there are a few things to note:
In your exmpmle it is fine to pass the argument as a const ref, because you don't change it.
and also in the cpp accordingly:
You don't need to allocate the object on the heap by using new at all. In fact you can almost always use sytax like that:
Whenever you habe lots of raw data, classical arrays in particular, those usually are class members (and even this is a rare case, because of the awesome stl containers like vector) and you let that class deal with the memory management. This again allows creating those classes on the stack, cleaning up by letting them go yout of scope and teh class will take care that the huge amount of data is actually stored on the heap.
So while your code is correct, you seldom actually have to worry about that because you