Discussion:
[Swig-user] c++ to c# vector of base class
Luca Tersi
2017-05-18 10:35:46 UTC
Permalink
Hi,
I'm new to SWIG. I need to create a std::vector of a base class and be
able to cast it to derived in c#. Is it even possible?
Do I need to use smart pointer? Where can I find a working example for this?
I can provide more details if needed.
Thank you in advance
Luca
schullq
2017-05-18 12:24:51 UTC
Permalink
Hi Luca,

Yes, is is possible, if u use pointer for sure. The rules of polymorphism
are respected.

For example, if u have:

class A
{
[...]
virtual void shout();
}

class B : public A
{
[...]
virtual void shout() override;
}

class C : public A
{
[...]
virtual void shout() override;
}

You can call shout from a std::vector<One *> and get the correct shout
implementation depending of the inheritance. It's the basic rule of C++ that
is repected.

Don't forget the "%template" macro in your interface file like:

%template(OnePtrVector) std::vector<One*>;

Hope I was clear enough.

Good luck,
Quentin.



--
View this message in context: http://swig.10945.n7.nabble.com/c-to-c-vector-of-base-class-tp15161p15162.html
Sent from the swig-user mailing list archive at Nabble.com.

Loading...