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

  • SEARCH
  • Home
  • 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 6824715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:56:06+00:00 2026-05-26T21:56:06+00:00

I have a class that looks like class Foo{ Foo(); Foo(int i); Foo(bool b);

  • 0

I have a class that looks like

class Foo{
    Foo();
    Foo(int i);
    Foo(bool b);
    Foo(double d);
};

and I expose my class to python as usual

class_<Foo>("Foo")
.def(init<int>())
.def(init<bool>())
.def(init<double>());

when I try to use to in python, the python code always cast the c’tor parameter into double (which is always the last one in the class def export). Is there a way to explicit tell boost.python how to explicitly handle by the type?

  • 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-26T21:56:07+00:00Added an answer on May 26, 2026 at 9:56 pm

    Well, you can change the order of constructor’s definitions, the last one will have higher priority. Here is my results:

    class_<Foo>("Foo")
    .def(init<bool>())
    .def(init<double>())
    .def(init<int>());
    
    Foo() # calls Foo()
    Foo(True) # calls Foo(int)
    Foo(1) # calls Foo(int)
    Foo(4.2) # calls Foo(double)
    

    As you see, it’s not a perfect solution. So, if you really need to make overloaded constructors work I suggest to roll your own factory function.

    using namespace boost::python;
    
    static boost::shared_ptr<Foo>
    makeFoo(const object& data)
    {
        boost::shared_ptr<Foo> obj;
    
        if (PyBool_Check(data.ptr())) {
            bool val = extract<bool>(data);
            obj.reset(new Foo(val));
        }
        else if (PyFloat_Check(data.ptr())) {
            double val = extract<double>(data);
            obj.reset(new Foo(val));
        }
        else {
            int val = extract<int>(data);
            obj.reset(new Foo(val));
        }
    
        return obj;
    }
    
    class_<Foo>("Foo")
        .def("__init__", make_constructor(makeFoo));
    

    And using makeFoo:

    Foo() # calls Foo()
    Foo(True) # calls Foo(bool)
    Foo(1) # calls Foo(int)
    Foo(4.2) # calls Foo(double)
    

    By the way, docs at python.org can be somewhat helpful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rack application that looks like this: class Foo def initialize(app) @app
Let's say I have a class that looks something like this: class Foo(Prop1:Int, Prop2:Int,
I have a class that looks like: class Foo { public: Foo(); virtual ~Foo();
I have a vector of that looks like the following: class Foo { //whatever
Let's say I have a bunch of classes that look something like... class Foo{
I have a class that looks like the following: public class MyClass { private
I have a class that looks like this: public class BasePadWI { public WorkItem
Currently I have a class that looks like this: public class MyClass : IMyClass
Assume I have a class that looks like this: class Sample { public string
If I have a class that looks like below, how to I pull out

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.