473,395 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Deriving from C++ classes in C#

I have a C++ class which I would like to use without changes in C#
applications. All the C++ class' definition is inline, i.e. the class
'library' just consists of a single header file. The class contains some
pure virtual functions. In a C# application I would now like to derive a
C# class from the C++ class and override the virtual functions.

Unfortunately I have not found a sample that is simple enough and
demonstrates the technique to do so. I succeeded with just calling C++
class methods from C#, but the tricky point is being able to derive a
class and override the virtual functions. I'm sure there must be a way.
Can anybody help me?

Thanks in advance
Urs

PS: For direct e-mail contact, please remove leading 'ns_' and trailing
'_ns' from e-mail address.

Nov 15 '05 #1
3 1647
Hi Urs,

You will have to modify your class in order to make it managed one as C#
classes can be inherited only from classes declared in managed code. To do
so, you will need a brief tutorial on Managed C++ - there should be kind of
migration guide from C++ to Managed C++ in the MSDN at least and I think
there's a number of tutorials on the Internet.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Urs Wigger" <ns_urs.wigger@leica-geosystems_ns.com> wrote in message
news:OX****************@TK2MSFTNGP09.phx.gbl...
I have a C++ class which I would like to use without changes in C#
applications. All the C++ class' definition is inline, i.e. the class
'library' just consists of a single header file. The class contains some
pure virtual functions. In a C# application I would now like to derive a
C# class from the C++ class and override the virtual functions.

Unfortunately I have not found a sample that is simple enough and
demonstrates the technique to do so. I succeeded with just calling C++
class methods from C#, but the tricky point is being able to derive a
class and override the virtual functions. I'm sure there must be a way.
Can anybody help me?

Thanks in advance
Urs

PS: For direct e-mail contact, please remove leading 'ns_' and trailing
'_ns' from e-mail address.


Nov 15 '05 #2
Hi Dmitriy,

Thanks for the answer. I succeeded to make the C++ class managed, derive
a C# class from it and can now call methods of the base (C++) class from
a C# application. However, I still didn't succeed virtual functions
getting treated correctly. In fact, calling a method of my class always
results in calling a virtual function (of the C++ class). I would like
to override this virtual function in my C# application. How do I have to
implement a virtual function of the C++ base class in the __gc wrapper
so that my overridden function in the C# application will be called
instead of the (C++) base class' implementation (That's what always
happens). Do you know an article or sample that handles this topic ?

Best Regards
Urs

Dmitriy Lapshin [C# / .NET MVP] wrote:
Hi Urs,

You will have to modify your class in order to make it managed one as C#
classes can be inherited only from classes declared in managed code. To do
so, you will need a brief tutorial on Managed C++ - there should be kind of
migration guide from C++ to Managed C++ in the MSDN at least and I think
there's a number of tutorials on the Internet.


Nov 15 '05 #3
The overriding method should be declared with the "overrides" keyword (and
NOT "virtual" - the compiler will keep silent if you do so but this won't
work as you want it to).

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Urs Wigger" <ns_urs.wigger@leica-geosystems_ns.com> wrote in message
news:eO**************@tk2msftngp13.phx.gbl...
Hi Dmitriy,

Thanks for the answer. I succeeded to make the C++ class managed, derive
a C# class from it and can now call methods of the base (C++) class from
a C# application. However, I still didn't succeed virtual functions
getting treated correctly. In fact, calling a method of my class always
results in calling a virtual function (of the C++ class). I would like
to override this virtual function in my C# application. How do I have to
implement a virtual function of the C++ base class in the __gc wrapper
so that my overridden function in the C# application will be called
instead of the (C++) base class' implementation (That's what always
happens). Do you know an article or sample that handles this topic ?

Best Regards
Urs

Dmitriy Lapshin [C# / .NET MVP] wrote:
Hi Urs,

You will have to modify your class in order to make it managed one as C#
classes can be inherited only from classes declared in managed code. To doso, you will need a brief tutorial on Managed C++ - there should be kind ofmigration guide from C++ to Managed C++ in the MSDN at least and I think
there's a number of tutorials on the Internet.


Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Steven T. Hatton | last post by:
This may be another question having an obvious answer, but I'm not seeing it. I'm trying to create a class that derives from std::valarray<std::string>. I don't need a template, and I haven't come...
2
by: Julian | last post by:
I would like to have output from my program to be written to cout as well as a file. (actually, i want several other output options but this should explain my problem in the simplest way). I have...
3
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to...
5
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to...
4
by: Confused Newbie | last post by:
I'm converting an app written in VB 2003 to 2005 and need advice for how to deal with this situation: The app has a number of "manager" classes that handle the data access. They all have...
15
by: Nindi73 | last post by:
HI If I define the class DoubleMap such that struct DoubleMap : public std::map<std::string, double>{}; Is there any overhead in calling std::map member functions ? Moreover are STL...
12
by: Shraddha | last post by:
Can I stop people by deriving my class? I mean I don't want my class to be as a base class... Can I do that?
1
by: Christopher Pisz | last post by:
I set out to make a custom logger. Examining some other code laying around, I came across one that derived from ostream, and had a associated class derived from streambuf. Is this practice a good...
0
by: James Kanze | last post by:
On 11 avr, 17:44, "mc" <mc_r...@yahoo.comwrote: OK. If the actual format is well documented, that's half the battle won already. Note, however, that reading a float as an int is still very...
3
by: Al Grant | last post by:
Consider two translation units, (1): namespace { struct S { }; } struct D: S { virtual int f(void); }; and (2): #include <typeinfo> struct D; char const *f(D *p) { return...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.