[C++-sig] problem with overloaded address operator

Fred Houben qhf at oce.nl
Fri Nov 26 16:01:42 CET 2004


Hello,
I'm using boost.python 1.30.2 with Python 2.3 and msvc6.
I want to wrap a struct that has an object as data member.
The problem is that the address operator has been overloaded and returns a data member address instead of the object address.
Personally I think its' bad practise to overload the address operator (especially in this manner) , but it didn't write the code and removing the operator isn't a option.

I've simplified the problem into a Hello world example:

#include <boost/python.hpp>
using namespace boost::python;

class Hello
{
public:
  char const* greet(){return "Hello, world";}
};

class World
{
public:
 char const* greet(){return hello.greet();}
 Hello* operator&() { return &hello; }
 Hello hello;
};

struct Data
{
 World world;
};

BOOST_PYTHON_MODULE(world)
{
class_<Hello>("Hello")
  .def("greet", &Hello::greet)
  ;
class_<World>("World")
  .def("greet", &World::greet)
  ;
class_<Data>("Data")
  .def_readonly("world", &Data::world)
  ;
}

this result in the following error:
pointer_holder.hpp(177) : error c2440: 'initializing" : cannot convert from 'class Hello *' to 'class World *'

I solved the problem by editing pointer_holder.hpp. Replacing Value* p = &*this->m_p; with boost::addressof(*this->m_p);.
This way I avoid using the address operator. This works fine, but maybe there's something I'm missing here, any input is welcome.
I'm forced to uses boost.python for now 1.30.2, but is this problem solved in other releases?

Thanks,
Fred Houben 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20041126/1654db0c/attachment.htm>


More information about the Cplusplus-sig mailing list