Assignment operator not being called

04/09/2015 17:57 Mi4uric3#1
Hi,

I'm trying to teach myself about how to use copy constructors and assignment operators.

I'm trying to realize a RAII class for handles.
I found this example here: [Only registered and activated users can see links. Click Here To Register...]

Now I'm trying to implement the assignment operator like this:

Code:
HandleType& operator=(const HandleType& value) {
	DuplicateHandle(reinterpret_cast<HandleType>(-1), value, reinterpret_cast<HandleType>(-1), &_value, 0, false, DUPLICATE_SAME_ACCESS);
	return *this;
}
The thing is, it never gets called.


processHandle_ should have a different value than processHandle because of the DuplicateHandle function, but it doesn't.

What am I doing wrong? Do you need to see more of the code?

Thanks in advance.
04/25/2015 12:01 MrSm!th#2
What is HandleType? Some kind of typedef or template parameter?
04/25/2015 15:41 Mi4uric3#3
Quote:
Originally Posted by MrSm!th View Post
What is HandleType? Some kind of typedef or template parameter?
The person on the StackOverflow-link I provided had it in his sourcecode:
Code:
template<typename H, BOOL _stdcall CloseFunction(H)>
class checked_handle {
public:
	typedef typename H HandleType;

When it comes to c++ templates, operator-overwriting and things like this I am very uneducated. I read about std::shared_ptr so I tried a different way which I think works how I want it to work.

The current implementation:
05/15/2015 09:16 MrSm!th#4
Sorry for the late response.

Although you already fixed your problem by working around it, I got one more question:

That typedef THandle<HANDLE> Handle in your solution, did you also use that in your original code, i.e. at this point:

[Only registered and activated users can see links. Click Here To Register...]

processHandle and processHandle_ are of the type THandle<HANDLE> in fact?

If this is the case, that's the reason why your assignment operator is not called. You defined it as
Code:
HandleType& operator=(const HandleType& value) {
	DuplicateHandle(reinterpret_cast<HandleType>(-1), value, reinterpret_cast<HandleType>(-1), &_value, 0, false, DUPLICATE_SAME_ACCESS);
	return *this;
}
meaning that it will be called, if you try to assign a HandleType (which is HANDLE in your case) to your handle wrapper. But if you assign a handle wrapper to another one, the default assignment operator will be used.

Code:
THandle& operator=(const THandle& other) {
	DuplicateHandle(reinterpret_cast<HandleType>(-1), other._value, reinterpret_cast<HandleType>(-1), &_value, 0, false, DUPLICATE_SAME_ACCESS);
	return *this;
}
would be correct.