c++ to c# vector of base class

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

c++ to c# vector of base class

Luca Tersi
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


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: c++ to c# vector of base class

schullq
This post was updated on .
Hi Luca,

Yes, is is possible, if you 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.

PS: I use simple pointers for the example, but smart pointers work too.