[C++-sig] How to add new constructor in a class

Roman Yakovenko roman.yakovenko at gmail.com
Sun Jun 20 06:16:03 CEST 2010


On Fri, Jun 18, 2010 at 1:58 PM, vishal bayskar
<vishal.bayskar at nechclst.in> wrote:
>
> In the below example I need to add a constructor A(). How to do that?
>
> class A{
> public:
>        A(int);
>        int getA();
> private:
>        int a;
> };
>

If you can modify source code - than just do it, otherwise you will
have to use make_constructor(
http://language-binding.net/pyplusplus/documentation/functions/make_constructor.html
) functionality.

> I have tried add_declaration_code and add_registration_code but it seems
> that these will not work for
> the case of contructor.
>
> Does these work and I am doing somthing wrong?

The functionality add_declaration_code and add_registration_code
works, unless you have a test case which shows the opposite.

>
> I also tried to create constructor using decl_factory_t.create_constructor()
> but I am not able to add the constructor in the wrapper code

This is the wrong approach, since the desired constructor doesn't
exist in the source code - so you will have a lot of problems later

> Please let me know the possible solution
>
> One more query Can I overwrite some constructor
> If suppose a constructor is declared as private can I change it's access as
> a public (or can I overwrite it somehow)?

#define private public
#include <your source file>

this ugly trick will allow you to achieve what you want, but it may
not work on some compilers/classes. You will have to check.

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/


More information about the Cplusplus-sig mailing list