Class constructor using another object
So, I have the following code that works great:
CMyClass& CMyClass::operator=(DWORD rhs)
...
CMyClass exc;
exc = GetLastError();
And it does whatever I expect (call stuff inside the statement =
). I was wondering how to do this so that I could write the following instead:
CMyClass exc = GetLastError();
I tried using the above and it doesn't call the operator functionality =
, it just leaves me with a class where only the default constructor was called.
thank
+3
source to share
1 answer
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 usually best to be explicit and write;
CMyClass exc ( GetLastError() );
+4
source to share