[C++-sig] Wrapping a class with private operator&()

Matthias Baas baas at ira.uka.de
Mon Oct 10 18:21:35 CEST 2005


David Abrahams wrote:
>>I'm having problems wrapping a C++ class that has a private reference 
>>operator (operator&()). 
> 
> Well, first of all, don't do that.  A class whose address-of operator
> doesn't do the normal thing is going to **break** just about every
> generic library you try to use it with in exactly this way.  It's
> generally agreed that overloading operator& is pure evil except in
> very special circumstances.  

This code was not written by myself, it's from an SDK I'm currently 
wrapping. The SDK belongs to Maya, a 3D modeling/animation tool by Alias 
(or soon by Autodesk).
I have no idea why they made the operators private. The assignment 
operators are also private, maybe they just want to prevent anybody from 
passing such instances around...?


>>Here is a minimal example that shows the problem:
> 
> In the enclosed patch, I replaced enough uses of "&" with addressof to
> make your example compile, but I'm almost certain there will be other
> such instances.  If you try it and it works for you, I'll check it in.

I've tried the patch with the original code and it seems to be fine. The 
code compiles and can be used in Python. Thanks!


The other suggestion by Dennis Brakhane to derive from the class and 
make the operator public is also a viable option that works as well. (I 
thought that the method would remain private just as it is the case with 
'virtual'...). So thanks again!

Regards,

- Matthias -




More information about the Cplusplus-sig mailing list