sách gpt4 ai đã đi

Trình biên dịch gọi toán tử sao chép thay vì toán tử di chuyển

In lại 作者:行者123 更新时间:2023-12-01 14:39:57 31 4
mua khóa gpt4 Nike

我事先确实在这里找到了一个确切的问题,但我没有相同的问题原因(或者至少我认为是这样)。我的 AnimatedSprite类有 Bộ đếm thời gian成员,使其不可复制(明确删除了复制 ctor 和 operator= )。然而,当我尝试在其他类的 ctor 中初始化 sprite 时,我收到一条错误消息,指出我正在引用已删除的复制 operator=。

计时器.h:

#pragma once
#include

class Timer
{
công cộng:
Timer() :
currentTime_{0},
expirationTime_{0}
{}
Timer(unsigned expirationTime) :
currentTime_{ expirationTime },
expirationTime_{ expirationTime }
{
timers_.insert(this);
}

~Timer() {
timers_.erase(this);
}
// Here
Timer(const Timer& other) = delete;
Timer& operator=(const Timer& other) = delete;

Timer(Timer&& other) = default;
Timer& operator=(Timer&& other) = default;


// Assumes the user knows if |this| is active or not
unsigned currentTime() const { return currentTime_; }

bool active() const { return currentTime_ < expirationTime_; }
bool expired() const { return !active(); }
void reset() { currentTime_ = 0; }
void disable() { currentTime_ = expirationTime_; }


static void updateAll(unsigned elapsedTime);

riêng tư:

void update(unsigned elapsedTime) {
if (active()) {
currentTime_ += elapsedTime;
}
}


static std::set timers_;
unsigned currentTime_;
const unsigned expirationTime_;

};

动画 Sprite .h
#pragma once
#include
#include
#include "Globals.h"
#include "Sprite.h"
#include "Timer.h"


class Sprite;
class Graphics;

class AnimatedSprite : public Sprite
{
công cộng:
AnimatedSprite() = default;
AnimatedSprite(Graphics& graphics, const std::string& filePath,
int sourceX, int sourceY, int width, int height,
unsigned frameLength, unsigned frameCount);

void update(bool once = false);
const bool completedOnce() const { return once_; }

riêng tư:

Timer frameTimer_;
unsigned currentFrame_{ 0 };
unsigned totalFrames_;
bool once_{ false }; // if true, plays animation once
};

然后我这样做:
sprite_ = AnimatedSprite( graphics, "Resource/NpcSym.png", 0, 1, 16, 16, 50, 5 );

不应该在这里调用 move 运算符吗?我的 RHS 不是在右值上方吗?

1 Câu trả lời

关于你的 AnimatedSprite防止它 move 。一旦消除,它就会尝试复制。你得到你的错误。
Sprite 中的可能内容阻止它被 move 。或 Bộ đếm thời gian 中的内容; =default可以变成=delete如果你不能 move 那个东西。

我使用的一种技术是注入(inject)有关此类假设的静态断言。

static_assert( std::is_move_assignable{}, "Cannot move Sprite" );
static_assert( std::is_move_constructible{}, "Cannot move Sprite" );
static_assert( std::is_move_assignable{}, "Cannot move Timer" );
static_assert( std::is_move_constructible{}, "Cannot move Timer" );

现在,使用我的脑内编译器,我可以看到:
const unsigned expirationTime_;

将阻止 Bộ đếm thời gian 上的 move 分配.

备份到设计:
sprite_ = AnimatedSprite( graphics, "Resource/NpcSym.png", 0, 1, 16, 16, 50, 5 );

将一个当前动画 Sprite 分配给另一个是否有意义?

我对此表示怀疑。

我会 =delete operator=(&&) .

替换 sprite_std::optional ,然后执行:
sprite_.emplace( graphics, "Resource/NpcSym.png", 0, 1, 16, 16, 50, 5 );

这通常比拥有一个“处于非 Sprite 状态的 Sprite ”要理智得多。

关于c++ - 编译器调用复制操作符而不是 move 操作符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59993710/

31 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com