Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6376043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:41:39+00:00 2026-05-25T01:41:39+00:00

I have a class method that returns a pointer to an inner data structure

  • 0

I have a class method that returns a pointer to an inner data structure (where the data structure is guaranteed to outlive its use in python code). It looks like:

class MyClass {
    ...

    some_structure* get() {
        return inner_structure_;
    }

    private:
    some_structure* inner_structure_;
};

I want to wrap this get() method in Boost::Python so that if two different objects of this class return the same pointer, the associated some_structure objects in python compare equal.

Inside the class_<MyClass> definition I’ve tried wrapping get() with both return_value_policy<reference_existing_object>() and return_inner_reference<>() call policies, but in both cases, calling get() on different python “MyClass” objects returns different some_structure objects even though all point to the same memory address in C++.

How would I get around this? Might there be a hidden property inside the wrapper object that stores the pointer’s address, so I can compare those instead?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:41:39+00:00Added an answer on May 25, 2026 at 1:41 am

    Figured out a way to do it, although it still feels hackish and that there should be some easier way. But here goes:

    1) Define your own methods that compare the pointers.

    template <typename T>
    bool eq(const T* self, const T* rhs) {                
      return self == rhs;                                                           
    }                                                                               
    
    template <typename T>                                                           
    bool ne(const T* self, const T* rhs) {                
      return !eq<T>(self, rhs);                                              
    }
    

    2) Manually declare the __eq__ and __ne__ inside the wrapper class to point to those methods:

    class_<smth>("smth", no_init)
      ...
      .def("__eq__", &eq<smth>)
      .def("__ne__", &ne<smth>);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class method that returns a list of employees that I can iterate
I have a class with a ToString method that produces XML. I want to
I have a .NET class library containing a class with a method that performs
I have a class that utilizes a directory swap method for the Environment.CurrentDirectory. The
Suppose that I have a Java class with a static method, like so: class
I have a base class with a virtual method, and multiple subclasses that override
You have a method on a class that has 2 parameters, one of which
We have a static method in a utility class that will download a file
I have seen that a useful way to write a clone method that returns
I have a class that contains two methods like these: public String getFoo(Int32 a)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.