程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何不隐式删除默认运算符?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何不隐式删除默认运算符??

开发过程中遇到如何不隐式删除默认运算符?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何不隐式删除默认运算符?的解决方法建议,希望对你解决如何不隐式删除默认运算符?有所启发或帮助;

我正在处理的代码产生多个线程,每个线程执行相同的一组明确定义的任务。那些明确定义的任务可以很好地分成模块,这就是我在 ATM 中失败的地方。

我有一个记录器类,它被实例化和初始化

class TLogger
{
    int SlotID{};
    States& State;
public:
    voID Init(int SlotID,States& StatE);
    voID Log(String linE);
    voID LogState();
};

原因是所有记录器都写入同一个日志,因此我也需要将 SlotID 写入日志。只要该特定线程运行,它就是不可变的。状态也需要记录,但它是可变的。它由线程函数(它创建并初始化它自己的记录器)改变。

我正在尝试创建一个在同一线程中运行并访问硬件的 HAL 实例。需要记录这些访问,因此我想为当前线程中运行的 HAL 实例提供对该线程记录器的引用

class THAL
{
    nHandle HarDWareHandle;
    TLogger& OwnLogger;
public:
    voID Init(nHandle HarDWareHandle,TLogger& Logger);
};

voID THAL::Init(nHandle HarDWareHandle,TLogger& Logger)
{
    this->HarDWareHandle= HarDWareHandle;
    this->OwnLogger = Logger;
}

编译器说

'TLogger &TLogger::operator =(const TLogger &)': attempTing to reference a deleted function
compiler has generated 'TLogger::operator =' here
'TLogger &TLogger::operator =(const TLogger &)': function was implicitly deleted because 'TLogger' has a data member 'TLogger::State' of reference type (compiling source file THAl.cpp)
see declaration of 'TLogger::State' (compiling source file THAl.cpp)

如果我没看错的话,编译器通过给我的类一个 =-operator 来做几乎正确的事情,但之后会杀死它。

除了切换到原始指针之外,如何将线程本地记录器的引用获取到线程本地 HAL 中?

如果我只有一个线程,我会将所有内容都声明为静态单例并收工,但唉,这不是一个带有可选边界条件的业余爱好项目。

解决方法

据我所知,除了问题中提到的问题之外,您当前的代码还有另一个问题。 TLogger 也不能有默认构造函数,因为不能用默认构造函数初始化非静态引用成员。我认为解决这个问题的一种方法是将 Init() 功能移动到类的构造函数(应该在使用前初始化对象)并使用成员初始化列表,如下所示:

#include <iostream>
#include <memory>
#include <String>
#include <thread>

class States{
    const static size_t SIZE = 1<<20;
    int a[SIZE];
    int dataLoad_ = 0;
    public:
    void setData(int X);
};

void States::setData(int X) {
    if(dataLoad_ < SIZE - 1) {
        a[dataLoad_++] = x;
    }
}

class TLogger
{
    size_t SlotID_ {};
    States& State_;
public:
    TLogger(int SlotID,States& StatE);
    void Log(std::string LinE);
    void LogState();
};

TLogger::TLogger(int SlotID,States& StatE):
SlotID_(SlotID),State_(StatE)
{}

void TLogger::Log(std::string LinE) {
    std::cout<<"[slot "<<SlotID_<<"] :"<<Line<<'\n';
}

class THAL
{
    using nHandle = int;
    nHandle HardwareHandle_;
    TLogger& Logger_;
public:
    THAL(nHandle HardwareHandle,TLogger& Logger);
    ~THAL();
};

THAL::THAL(nHandle HardwareHandle,TLogger& Logger):
HardwareHandle_(HardwareHandlE),Logger_(Logger)
{}

THAL::~THAL() {
    Logger_.Log("Closing device " + std::to_String(HardwareHandle_));
}

void runThread(States* state,int X) {
    // int slotId(std::hash<std::thread::id>(std::this_thread::get_id()));
    int slotId = std::hash<std::thread::id>{}(std::this_thread::get_id());
    TLogger logger(slotId,*statE);
    THAL hw(1,logger);
}

int main() {
    States* state = new States;
    state->setData(5);
    std::thread t(runThread,state,1);
    std::thread t2(runThread,2);
    t.join();
    t2.join();
}

可能的输出:

[slot 18446744072947625951] :Closing device 2
[slot 18446744071970489560] :Closing device 1

大佬总结

以上是大佬教程为你收集整理的如何不隐式删除默认运算符?全部内容,希望文章能够帮你解决如何不隐式删除默认运算符?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: