LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Mono and C++ (https://www.linuxquestions.org/questions/programming-9/mono-and-c-705849/)

kpachopoulos 02-19-2009 04:38 AM

Mono and C++
 
Hi,
i have learned only standard C++ with gcc and i would like to get involved in C++ Windows development. I have understood, that Mono doesn't support C++/CLI - the "new" version, but supports managed C++.
Is this possible to run this sample program, found in the internet? I would like to learn how, step-by-step. Is there a link? I have not found something... I am a newbie and any help will be appreciated.

Thanks

Code:

#using <mscorlib.dll>

using namespace System;

// A simple class

__gc class CHelloWorld
{
  System::String *_msg;
public:
  CHelloWorld(System::String *Msg)
  {
    _msg = Msg;
  }

  void Print()
  {
    Console::WriteLine(_msg);
  }
};

// Yet another class w/ but a static constructor

__gc class CHelloWorldStatic
{
  static System::String *_static_msg =
    S"Static message called because static constructor invoked!";
  static Int32 _instance_cnt = 0;

public:

  // constructor

  CHelloWorldStatic()
  {
    _instance_cnt++;
    Console::WriteLine(System::String::Concat("So far ",
            _instance_cnt.ToString(), " instance(s)"));
  }

  // static constructor making use of the static member

  static CHelloWorldStatic()
  {
    Console::WriteLine(_static_msg);
  }

};

int main()
{
  // create an instance of the class

  CHelloWorld *c1 = __gc new CHelloWorld(S"Hello world from the class");

  c1->Print();

  // take an instance of the class that has static constructor

  // now we will notice that first, the static constructor

  // will be called, then the ordinary constructor

  CHelloWorldStatic *c2 = __gc new CHelloWorldStatic();

  return 0;
}


paulsm4 02-19-2009 11:14 PM

Hi -

Programming a .Net application in C++ is kind of like programming a Java application in Ada. You can *do* it (http://code.google.com/p/jgnat/) ... but why bother?

Despite Microsoft's claim of being "language agnostic" (which, btw, isn't "untrue"...) ... VB.Net and C# are really the only two languages I'd recommend (and of course ASP.Net for server-side web pages).

Conversely, coding "standard C++" (managed or unmanaged code) has become increasingly unpleasant with newer versions of VS2008 - I'd prefer GCC for C/C++ any day :-)

All my own opinions (for whatever it's worth): but I'd definitely recommend *not* trying to program .Net on C++.

IMHO .. PSM

PS:
Your code snippet is .Net-to-the-max: I have no idea how I'd go about trying to compile, link and run it with Gcc (for example).

PPS:
You might also be interested in looking at Microsoft's "Visual Studio Express" offerings here (they're perfectly functional, and great tools):

http://www.microsoft.com/express/


All times are GMT -5. The time now is 11:51 AM.