我对在继承层次结构中移动构造函数有疑问。在C++ Primer (Stanley )中提到,继承层次结构中的move构造函数定义如下:
class Base { /* Default Copy control and move constructor */ };
class D : public Base {
public:
D(const D& d) : Base(d) {/*initializers for members of D */} //This is ok
D(D&& d): Base(std::move(d)) {/*initializers
我不断地收到以下错误消息,告诉我正在使用一个deleted函数,我认为它是std::variant默认构造函数。 In file included from main.cpp:2:
Document.hpp: In instantiation of ‘Document<StateVariant, EventVariant, Transitions>::Document(StateVariant&&) [with StateVariant = std::variant<DraftState, PublishState>; EventVariant = s
编译器提醒我正在使用一个已删除的函数。
#include <memory>
using namespace std;
class foo
{
public:
unique_ptr<int> p;
~foo()
{
}
};
int main()
{
foo a, b;
a = move(b);
return 0;
}
编译信息
prog.cpp: In function 'int main()':
prog.cpp:15:4: error: use of deleted function '
请考虑以下课程:
class A
{
public:
std::string field_a;
std::string field_b;
}
现在考虑下面的复制构造:
A a1(a2);
尽管缺少显式的复制构造函数,但复制构造器将充分地复制A,因为std::string的复制构造函数将由编译器生成的隐式复制构造函数调用。
我想知道的是,移动建筑也是如此吗?
编辑:测试显示:
A a2(std::move(a1));
实际上将导致复制构造,除非特定的移动构造函数:
A( A && other ) : a(std::move(other.a)) {}
被定义为。
编辑,
我是C++的新手。我经常在一些网站上看到这个变种成员?
class School
{
int x; -> data member.
}
我认识数据会员。但是什么是变体成员呢?
注意:来自c++规范:在构造器页面下。
X is a union-like class that has a variant member with a non-trivial default constructor.
以下代码将导致编译器错误:
#include <optional>
class A {
};
class B {
private:
const A a;
};
int main()
{
B b;
std::optional<B> bo1;
bo1 = b;
}
例如,在gcc上,错误为:
main.cpp: In function 'int main()':
main.cpp:12:7: error: uninitialized const member in 'class B'
12 | B
隐式移动构造函数到底是做什么的?例如,对于下面的类,隐式移动构造函数会是什么样子(可以提供这个隐式构造函数的一些示例实现):
struct A
{
A() = default;
A(A && other) = default;
int a;
};
struct B : public A
{
int b;
int * c;
};
实现是否如下所示:
B(B && other) : A(std::move(other)), b(std::move(other.b)), c(std::move(other.
所以我有一个which类,用来表示棋盘上的棋子,我计划从它继承另外两个类。然而,我在这样做的过程中遇到了很多问题;以下是到目前为止的相关代码。
///
/// PIECE CLASS HERE
/// this is an abstract class from which Barrier and Pawn inherit.
class Piece
{
public:
Piece(Space* start);
sf::Shape m_Circle;
protected:
int m_X;
int m_Y;
int m_radius;
Space
谁能解释一下如何将字符串赋值并将其推入向量(在结构内部的联合中)?这个是可能的吗?向量是否试图访问无效内存?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef struct {
int height;
int width;
} Page;
typedef struct varstruct{
int test;
union u
在C++11中,如果基类已经定义了自己的move (copy)构造函数(赋值操作符),那么它的子类是否需要定义自己的move (copy)构造函数(赋值操作符),其中调用基类的相应构造函数/操作符被显式调用?
每次清晰地定义构造函数、析构函数、移动/复制构造函数(赋值操作符)是个好主意吗?
struct Base {
Base() {}
Base(Base&& o);
};
struct Sub : public Base {
Sub(Sub&& o) ; // Need I do it explicitly ? If not,wha
如何在C++中定义类的非静态常量数据成员?如果我尝试编译以下代码:
class a
{
public:
void print()
{
cout<<y<<endl;
}
private:
const int y=2;
};
int main()
{
a obj;
obj.print();
}
我得到一个错误
ISO C++ forbids initialization of member ‘y’
我试着模拟某个游戏作为一个练习(不要问我哪一个,你会知道你是否知道这个游戏),然而,我只是在网上学习了物体和课程,整个事情仍然让我很困惑。下面是我的代码中给出错误的部分。
我的错误是:
C:\Users\N\Desktop\Untitled1.cpp In constructor 'ekop::Moveset::Moveset()':
56 9 C:\Users\N\Desktop\Untitled1.cpp [Error] no matching function for call to 'Move::Move()'
56 9 C:\U