C++超载<<操作员问题
我对 C++ 相当菜鸟,但对指针、取消引用等非常熟悉。我对 << 的重载有问题。类的运算符,因为它可以正常编译,但运行时会崩溃。感觉就像无限循环,但我不确定。这是代码,感谢任何帮助。
#include <string>
#include <iostream>
using namespace std;
class Person
{
private:
string _name;
Person* _manager;
public:
Person(string name, Person *manager);
Person(string name);
friend ostream &operator<<(ostream &stream, Person &p);
};
Person::Person(string name, Person *manager)
{
_name = name;
_manager = manager;
}
Person::Person(string name)
{
_name = name;
}
ostream &operator<<(ostream &stream, Person &p)
{
Person* mgr = p._manager;
stream << p._name << std::endl;
stream << mgr->_name << std::endl;
return stream;
}
int main()
{
Person *pEmployee = new Person("John Doe Employee");
Person *pManager = new Person("John Doe Manager", pEmployee);
cout << *pEmployee;
cout << *pManager;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在只有一个参数的构造函数中,您需要将 _manager 设置为 NULL/0。在您的操作员中对此进行测试<<如果 mgr->name 为 NULL/0,则不输出。就目前情况而言,您正在取消引用未初始化的指针。
还有许多其他事情您可以做得更好,例如对参数使用 const 引用和使用构造函数初始值设定项列表,但它们不会成为问题的原因。您还应该解决传递给构造函数的管理器对象的所有权问题,以确保它不会被双重删除。
In your constructor with just a single argument, you need to set _manager to NULL/0. Test for this in your operator<< and don't output mgr->name if it is NULL/0. As it stands, you are dereferencing an uninitialised pointer.
There are a number of other things you could do better, such as using const references on arguments and using the constructor initialiser list, but they wont be the cause of your problem. You should also address ownership issues with the manager object you pass in to the constructor to ensure it does not get double-deleted.
您的 _manager 指针在构造第一个 Person 实例时未初始化,因此在您的运算符 << 中引用 p._manager崩溃。
除此之外,由于您调用 new 但不调用 delete,因此会出现内存泄漏。
your _manager pointer is not initialized upon constructing the first Person instance, hence referencing p._manager in your operator << crashes.
Besides that, you have a memory leak since you call new but not delete.
您的
operator<<
实现不会检查_manager
成员是否有效。如果没有管理器,您应该通过将指针设置为0
来明确这一点。否则,指针值未定义,访问它会使程序崩溃。(1) 如果构造函数中未提供任何内容,则您的
Person
类应将_manager
设置为0
(空指针):(2) 在您的 < code>operator<<,在取消引用之前检查指针:
更好代码的一些提示:
(1) 将函数参数更改为接受
const string&
而不是string< /代码>。这样,在调用函数/构造函数时不会复制字符串,而是作为常量引用传递。
(2) 让您的
operator<<
也接受Person
的const
引用会更干净,因为它不/应该不修改Person
。这样,您也可以在有常量Person
的地方使用该运算符。Your implementation of
operator<<
does not check if the_manager
member is valid. If there is no manager, you should make this explicit by setting the pointer to0
. Otherwise the pointer value is undefined and accessing it will crash your program.(1) Your
Person
class should set_manager
to0
(null pointer) if none is supplied in the constructor:(2) In your
operator<<
, check the pointer before dereferencing it:Some hints for better code:
(1) Change your function arguments to accept
const string&
instead ofstring
. This way, the string is not copied when calling the function/constructor, but passed as a constant reference.(2) It is cleaner to let your
operator<<
accept aconst
reference for thePerson
as well, since it does not/should not modify thePerson
. This way, you can use the operator also in places where you have a constantPerson
.你的经理是谁?
Who is your manager?
您的 Person 实例 *pEmployee 没有设置 _manager。这可能是一个 NULL 指针。
Your Person instance *pEmployee does not have the _manager set. This may be a NULL pointer.