如何在我的构造函数中将 shared_ptr 初始化为 QTextCodec?

How to initialize a shared_ptr to a QTextCodec in my constructor?

我正在尝试在我的 class 构造函数中将 shared_ptr 初始化为 QTextCodec(Qt class 用于字符集转换)。

这是我得到 ‘virtual QTextCodec::~QTextCodec()’ is protected within this context 错误的代码:

myencoder.h

#ifndef MYENCODER_H
#define MYENCODER_H

#include <memory>
#include <QTextCodec>

class MyEncoder
{
    std::shared_ptr<QTextCodec> m_codec;

public:
    MyEncoder(QString &aCodec);
};

#endif // MYENCODER_H

myencoder.cpp

#include "myencoder.h"

MyEncoder::MyEncoder(QString &aCodec)
{
    m_codec = std::shared_ptr<QTextCodec> (QTextCodec::codecForName(aCodec.toLatin1()));
}

如何在 MyEncoder 的构造函数中初始化 m_codec 属性?

来自docs

QTextCodec::~QTextCodec ( )     
                              protected virtual

Destroys the QTextCodec. You should not delete codecs. Once created their lifetime becomes the responsibility of CopperSpice.

incorporated version相同:

QTextCodec::~QTextCodec()               
                             [virtual protected]

Destroys the QTextCodec. Note that you should not delete codecs yourself: once created they become Qt's responsibility.

所以也许将空删除器添加到您的 shared_ptr 或使用原始指针并将其留给库。