Cluebat-man to the rescue

A weblog dedicated to Visual C++, interoperability and other stuff.

Differences between C# and C++/CLI, part 2

One question that comes up from time to time in the C++ newsgroups is that functions or constants that exist in a C++/CLI class library are not accessible in a C# or VB.NET project.

The reason for this is nearly always that that constants or functions are defined outside of a class scope.

Consider the following - perfectly valid - C++/CLI:

namespace Bla

{

  const int CONST = 42;

  int func(void)

  {

    return CONST;

  }

};

You can use CONST and func in other C++/CLI code.

What you can’t do is to use those symbols in other .NET languages. The reason is that those languages do not have a concept of things existing outside of a class or struct scope.

In other words, even though the CLR supports it, languages other than C++/CLI do not support free standing functions, variables and constants.

A simple workaround is to put them in a dummy class. For example, you could put all constants in a class called ‘Consts’, and functions in a class called ‘Functions’. 

Posted: Aug 31 2006, 03:58 PM by vanDooren | with 1 comment(s)
Filed under: , ,

Comments

Anuj said:

would u please tell me the diifference b/w c++ and c++/cli?

# July 1, 2008 1:03 AM