Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# and C++ class inheritance intermingling

I have an interesting stack of assemblies I want to put together:

  1. Common Assembly (C# or C++-CLI)

    public class MyBase
    {
    public void MethodA()
    { ... }
    private void MethodB()
    { ... }
    protected virtual MethodC()
    { ... }
    }
    
  2. Test Code Assemblies (all C++-CLI)

    public class MySpecific : public MyBase{
    protected: override MethodC();
    };
    
  3. Test Simulator (C#)

    MySpecific obj = new MySpecific();
    obj.MethodC();
    

While assembly 1 could be C++-CLI to keep things simpler, I'd really like to keep assembly 3 in C#. This is largely an exercise to see if inheritance could be done in either direction, but I also have a real-world case where this stack is useful.

The first problem I find is that the C++-CLI assembly does not compile because it doesn't recognize MyBase as a class, even though I have a reference to assembly 1 and what looks like the proper namespace.

How do I write classes that are language-portable?

like image 725
Foozinator Avatar asked Aug 14 '09 16:08

Foozinator


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


3 Answers

I think you have to declare MySpecific as a managed class like this:

public ref class MySpecific : public MyBase { ... }

See the CLI/C++ documentation for details. You will not be able to use old C++ code without modifications, but I think everything you have in mind should be possible.

like image 146
Achim Avatar answered Sep 28 '22 11:09

Achim


Found the answer:

`#using "..\common\bin\debug\common.dll"`

That, in addition to the /clr switch (the c++ side needs to be mananged) seems to be working, so far.

like image 43
Foozinator Avatar answered Sep 28 '22 11:09

Foozinator


This will work fine, but you'll need to use the C++/CLI syntax, if you're working with managed types (ie: inheriting from a C# class). So, in this case, the item in 2. should look more like:

public ref class MySpecific : public MyBase { ... }

Make sure that file is compiled with /clr are well.

Here is a tutorial describing inheritance in C++/CLI.

like image 26
Reed Copsey Avatar answered Sep 28 '22 12:09

Reed Copsey