2

I want to expose a C++ friend functions to python using Python boost.

     class Turtle{
               friend const PV& GetHeading(const Turtle& t);
               friend const PV& GetLeft(const Turtle& t);
               friend const P& GetPoint(const Turtle& t);
               friend void SetPoint(Turtle& t, const P& p);
               public:

               ...  

               private:
               PV h; 
               PV l; 

          };

Here I have wrapped the classes PV and P so no problem there. I tried to wrap the friend functions like regular functions. Like

          BOOST_PYTHON_MODULE(TurtleWrapper)
              {
                 class_<Turtle>("Turtle") 
                   .def("GetHeading",&Turtle::GetHeading) 
                   .def("GetLeft",&Turtle::GetLeft)
                   .add_property("h",&Turtle::GetHeading)
                   .add_property("l",&Turtle::GetLeft);
             }

When i run the code i get error messages.

           error: ‘GetHeading’ is not a member of ‘Turtle’
           error: ‘GetLeft’ is not a member of ‘Turtle’

So I assume that this is not the way to declare a friend function and the documentation of python boost does not seem (or atleast I did not see a note about friend functions).Any help is much appreciated.

user665903
  • 69
  • 6
  • Are you trying to use `friend` keyword where you don't need to? http://www.cplusplus.com/doc/tutorial/inheritance/ Declaring function as `friend` usually means it is not part of your class, it is a way to allow functions _outside_ of your class to access private/protected members. The errors, therefore, are valid. – sneg Jul 16 '14 at 11:57
  • I mean the C++ code is not written by me and it is a part of a larger code and these functions are indeed used as friends for various classes so that they can access the data members of those classes. So I have no idea whether to wrap them or not. but they are also used in this class named Turtle. So my question is can it be wrapped in to python boost and if yes that how can i do it? – user665903 Jul 16 '14 at 12:09

1 Answers1

1

Looking just at this fragment of the code, it is unclear why functions are declared as friend in the first place.

friend keyword is used to indicate to the compiler that a given function or class is able to access private and protected members of the class for which it is defined. friend functions are not members of the class.

When you bind your functions, you specify, for example, &Turtle::GetLeft as a pointer to the function you wish to bind. GetLeft, however, is not a member of Turtle, it is simply a friend function.

Looking at your binding, what you want to do is make functions such as GetHeading/GetLeft public member functions.

Therefore, I assume you have something like this:

class Turtle {
  friend const PV& GetHeading(const Turtle& t);
  // ...
};
// somewhere else in your code
const PV& GetHeading(const Turtle& t) {
  // getting heading here
}

I suggest re-writing like this:

class Turtle {
  public:
  // no need for friend declaration
  const PV& GetHeading() const {
    // move code into the class
  }
  // ...
};

Once you re-write your functions as public member functions instead of friend non-member functions, you will be able to bind them as above.

It is probably worth noting that it is good practice to use friend sparingly. If used incorrectly, it can break encapsulation of your class. Have a look at When should you use 'friend' in C++?

Community
  • 1
  • 1
sneg
  • 2,088
  • 4
  • 19
  • 23