C++ - 在创建时调用赋值运算符而不是复制构造函数
我想强制在结构之间进行显式转换,就像本机类型一样:
int i1;
i1 = some_float; // this generates a warning
i1 = int(some_float): // this is OK
int i3 = some_float; // this generates a warning
我想使用赋值运算符和复制构造函数来执行相同的操作,但行为不同:
Struct s1;
s1 = other_struct; // this calls the assignment operator which generates my warning
s1 = Struct(other_struct) // this calls the copy constructor to generate a new Struct and then passes that new instance to s1's assignment operator
Struct s3 = other_struct; // this calls the COPY CONSTRUCTOR and succeeds with no warning
是否有任何技巧可以获取第三种情况 Struct s3 = other_struct;
用默认构造函数构造s3,然后调用赋值运算符?
这一切都按预期编译和运行。 C++ 的默认行为是在创建新实例时调用复制构造函数而不是赋值运算符并立即调用复制构造函数,(即 MyStruct s = other_struct;
变成 MyStruct s(other_struct)
; 不是 MyStruct s; s = other_struct;
。显式”关键字正是我所需要的!
class foo {
foo(const foo& f) { ... }
explicit foo(const bar& b) { ... }
foo& operator =(const foo& f) { ... }
};
foo f;
bar b;
foo f2 = f; // this works
foo f3 = b; // this doesn't, thanks to the explicit keyword!
foo f4 = foo(b); // this works - you're forced to do an "explicit conversion"
I want to enforce explicit conversion between structs kind of like native types:
int i1;
i1 = some_float; // this generates a warning
i1 = int(some_float): // this is OK
int i3 = some_float; // this generates a warning
I thought to use an assignment operator and copy constructor to do the same thing, but the behavior is different:
Struct s1;
s1 = other_struct; // this calls the assignment operator which generates my warning
s1 = Struct(other_struct) // this calls the copy constructor to generate a new Struct and then passes that new instance to s1's assignment operator
Struct s3 = other_struct; // this calls the COPY CONSTRUCTOR and succeeds with no warning
Are there any tricks to get that third case Struct s3 = other_struct;
construct s3 with the default constructor and then call the assignment operator?
This all compiles and runs as it should. The default behavior of C++ is to call the copy constructor instead of the assignment operator when you create a new instance and call the copy constructor at once, (i.e. MyStruct s = other_struct;
becomes MyStruct s(other_struct)
; not MyStruct s; s = other_struct;
. I'm just wondering if there are any tricks to get around that.
EDIT: The "explicit" keyword is just what I needed!
class foo {
foo(const foo& f) { ... }
explicit foo(const bar& b) { ... }
foo& operator =(const foo& f) { ... }
};
foo f;
bar b;
foo f2 = f; // this works
foo f3 = b; // this doesn't, thanks to the explicit keyword!
foo f4 = foo(b); // this works - you're forced to do an "explicit conversion"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
免责声明:我准备对此投反对票,因为这并不能回答问题。但这对OP可能有用。
我认为将复制构造函数视为默认构造+赋值是一个非常糟糕的主意。反之亦然:
以这种方式实现是万无一失的,并且是您在 C++ 中的转换语义方面可以获得的最佳方式,因此这是这里的方法。
Disclaimer: I'm ready to take the downvotes on this, since this doesn't answer the question. But this could be useful to the OP.
I think it is a very bad idea to think of the copy constructor as default construction + assignment. It is the other way around:
Implementing things that way is fool proof, and is the best you can obtain in term of conversion semantics in C++, so it is the way to go here.
如果您重载 other_struct 的类型转换运算符,并相应地编辑原始结构,则可以解决此问题。也就是说,它非常混乱,而且通常没有充分的理由这样做。
输出:
You can get around this if you overload the type cast operator for other_struct, and edit the original structure accordingly. That said, it's extremely messy and there generally isn't a good reason to do so.
Outputs:
简而言之,不。
长版……其实就是这样。事实并非如此。但必须想出一些东西来满足角色要求。
In short, no.
The long version...actually that's about it. That's just not how it works. Had to come up with something to fill the character requirement though.
我不这么认为。当你编写
它时,它看起来像一个赋值,但实际上它只是调用构造函数的声明性语法。
I don't think so. When you write
It looks like an assignment, but really it's just declarative syntax that calls a constructor.