C++ 的签名带有枚举的结构的复制构造函数

发布于 2025-01-01 20:26:13 字数 362 浏览 0 评论 0原文

包含枚举的结构的隐式复制构造函数是否有理由使用const版本 - 或者为什么根本没有复制构造函数?我希望创建一个隐式复制构造函数,即:

X(const X& x)

我知道何时发生这种情况有规则,例如,如果成员变量没有复制构造函数或非常量复制构造函数。我想我的问题是这与枚举有何关系 - 这条规则是否适用?

添加我自己的复制构造函数似乎可行。

示例 - 隐式创建什么复制构造函数(如果有):

struct MyStruct {
  int myInt;
  double myDouble; 
  MyEnum myEnum;
};

Is there a reason why the implicit copy constructor for a struct containing an enum would not use the const version - or why there is no copy-constructor at all? I would expect an implicit copy-constructor being created, i.e.:

X(const X& x)

I know there are rules for when this might happen, for instance if a member variable does not have a copy constructor, or a non-const copy constructor. I guess my question is how this relates to enums - and if it is this rule that applies?

Adding my own copy constructor seems to work.

Example - what, if any, copy-constructors are created implicitly:

struct MyStruct {
  int myInt;
  double myDouble; 
  MyEnum myEnum;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花落人断肠 2025-01-08 20:26:13

您对枚举的猜测是错误的;问题出在别的地方。如果没有显式定义复制构造函数,则以下代码可以正常编译,如果存在采用非常量引用的复制构造函数,则编译失败。

#include <iostream>
#include <iterator>
#include <vector>

enum MyEnum {
  e0, e1, e2
};

struct MyStruct {
  int myInt;
  double myDouble; 
  MyEnum myEnum;
  // MyStruct(MyStruct& ){} // uncomment to make compilation fail
};

std::ostream& operator<<(std::ostream& out, const MyStruct& s) {
  out<<"{"<<s.myInt<<","<<s.myDouble<<","<<s.myEnum<<"}";
  return out;
}

int main() {
  MyStruct s = {42, 3.1415926, e0};
  MyStruct s1 = s;
  std::vector<MyStruct> v(10, s1);
  std::copy(v.begin(), v.end(), std::ostream_iterator<MyStruct>(std::cout,"\n"));
  return 0;
}

正如其他人在评论中指出的那样,需要一个证明错误的现实示例才能了解真正的问题是什么。

Your guess about enums is wrong; the problem is somewhere else. The following code compiles with no problem if no copy constructor is explicitly defined, and fails to compile if there is a copy constructor taking a non-const reference.

#include <iostream>
#include <iterator>
#include <vector>

enum MyEnum {
  e0, e1, e2
};

struct MyStruct {
  int myInt;
  double myDouble; 
  MyEnum myEnum;
  // MyStruct(MyStruct& ){} // uncomment to make compilation fail
};

std::ostream& operator<<(std::ostream& out, const MyStruct& s) {
  out<<"{"<<s.myInt<<","<<s.myDouble<<","<<s.myEnum<<"}";
  return out;
}

int main() {
  MyStruct s = {42, 3.1415926, e0};
  MyStruct s1 = s;
  std::vector<MyStruct> v(10, s1);
  std::copy(v.begin(), v.end(), std::ostream_iterator<MyStruct>(std::cout,"\n"));
  return 0;
}

As others pointed out in comments, a realistic example demonstrating the error is required to understand what's the real problem.

江心雾 2025-01-08 20:26:13

这有点超出我的专业知识。

当您尝试从 X 的非常量左值实例构造 X 时,签名 X(X& ) 将更接近匹配,并被选中。

This is a little bit out of my expertise.

When you try to construct an X from a non-const lvalue instance of X, the signature X(X& ) would be a closer match, and be chosen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文