简体   繁体   English

如何在构造函数中正确初始化wchar_t指针并在析构函数中将其删除

[英]How to initialize wchar_t pointer correctly in constructor and delete it in destructor

I'm trying to initialize a wchar_t* in my class. 我正在尝试在类中初始化wchar_t*

This is my constructor: 这是我的构造函数:

Book::Book()
{
    book = 0;
    auth = 0;
    setBook(TEXT(""));
    setAuth(TEXT(""));
}

And this is my destructor: 这是我的析构函数:

Book::~Book()
{
    if (book)
        delete book;
    if (auth)
        delete auth;
}

and here is setBook() : 这是setBook()

void Book::setBook(TCHAR *a) {
    if (book)
        delete book;
    book = new TCHAR[lstrlen(a) + 1];
    lstrcpy(book, a);
}

The problem is that when i'm trying to create new object and push it to vector, I get a run time error "The program has triggered a breakpoint" when delete book in destructor. 问题是,当我尝试创建新对象并将其推到矢量时,在析构函数中删除书籍时出现运行时错误“程序已触发断点”。

Here is my vector: 这是我的载体:

for (int i = 0; !feof(f); i++) {
        reqBook.push_back(Book());
        reqBook[i].read(f);
    }

Please any idea about my problem, or how to initialize correctly?. 请对我的问题有任何想法,或者如何正确初始化?

If you manage resources, you must have a copy constructor and a copy assignment operator (or delete them). 如果管理资源,则必须具有副本构造函数和副本分配运算符(或删除它们)。 This is The Rule of 3. With C++11, it's a good idea to have a move constructor and a move assignment operator too (the rule of 5). 这是3的规则。在C ++ 11中,最好有一个move构造函数和一个move赋值运算符(5的规则)。

If you try to make Book the class that manages resources, you will find there are some complexities involved with exception safety when you are part-way through copying or constructing book or auth . 如果尝试使Book成为管理资源的类,则在复制或构造bookauth ,您会发现异常安全涉及一些复杂性。

The solution is to write a RAII class which encapsulates handling an array of wchar_t , and make both book and auth be objects of that class. 解决方案是编写一个RAII类,该类封装处理wchar_t数组的处理,并使bookauth成为该类的对象。 When you have done that, you will find that you have made a poor-man's copy of std::wstring . 完成此操作后,您会发现您已经制作了一个穷人的std::wstring副本。

The only reason not to use std::wstring for this, is if it is a class assignment where you have been specifically forbidden from using it. 对此不使用std::wstring唯一原因是,如果它是特别禁止您使用的类分配。

The specific reason for your crash, is that std::vector copies your original Book object. 发生崩溃的具体原因是std::vector复制了原始的Book对象。 When it does so, it uses the compiler generated copy constructor (because you haven't provided one). 这样做时,它将使用编译器生成的副本构造函数(因为您没有提供一个)。 This just copies the pointers. 这只是复制指针。 The destructor of the original object deletes the memory these pointers are pointing at, and now you are in the world of pain known as "undefined behaviour". 原始对象的析构函数将删除这些指针所指向的内存,现在您处在痛苦的境地,即“未定义的行为”。 When the object in the vector is deleted, it too will try to delete the same memory, and anything can happen - hitting a breakpoint is a particularly painless result. 当向量中的对象被删除时,它也会尝试删除相同的内存,并且任何事情都可能发生-达到断点是一个特别轻松的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM