Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 08:06

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Assignment operator not being called

Discussion on Assignment operator not being called within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
Mi4uric3's Avatar
 
elite*gold: 405
Join Date: Dec 2007
Posts: 6,615
Received Thanks: 6,356
Assignment operator not being called

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:

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.
Mi4uric3 is offline  
Old 04/25/2015, 12:01   #2


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,904
Received Thanks: 25,394
What is HandleType? Some kind of typedef or template parameter?
MrSm!th is offline  
Old 04/25/2015, 15:41   #3
 
Mi4uric3's Avatar
 
elite*gold: 405
Join Date: Dec 2007
Posts: 6,615
Received Thanks: 6,356
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:
Mi4uric3 is offline  
Old 05/15/2015, 09:16   #4


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,904
Received Thanks: 25,394
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:



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.
MrSm!th is offline  
Reply




All times are GMT +2. The time now is 08:06.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.