简体   繁体   中英

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() );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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