简体   繁体   English

使用其他对象的类构造函数

[英]Class constructor using other object

So I have the following code which works nicely: 所以我有以下代码可以很好地工作:

CMyClass& CMyClass::operator=(DWORD rhs) 

...

CMyClass exc;
exc = GetLastError();

And it does everything I expect it to (call the stuff inside the = operator.) I was wondering how to get it so that I can instead write it like the following: 并且它完成了我期望的所有事情(调用=运算符内的东西。)我想知道如何获取它以便我可以像下面那样编写它:

CMyClass exc = GetLastError();

I tried using the above and it doesn't call the = operator functionality, instead just leaving me with a class where just the default constructor has been called. 我尝试使用上面的内容并没有调用=运算符功能,只是让我只有一个类,其中只调用了默认构造函数。

Thanks 谢谢

A constructor is required. 构造函数是必需的。

CMyClass(DWORD rhs)

Or explicit 或明确

explicit CMyClass(DWORD rhs)

Be warned , the implicit constructor allows this to compile; 请注意 ,隐式构造函数允许这样编译;

CMyClass exc = GetLastError();

But it also participates in compiler generated implicit constructions and conversions. 但它也参与编译器生成的隐式构造和转换。 It is generally better to have to be explicit and write; 通常最好是明确地写作;

CMyClass exc ( GetLastError() );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM