Thread: C++/CLI oddities

  1. #1
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972

    C++/CLI oddities

    I just got a C++/CLI book a couple days ago, and so far there are a few things that confuse me (the first four aren't really important, just confusing MS decisions).

    1. First of all, why is there both an enum struct and an enum class if they're both the same.

    2. Why did MS remove default arguments for ref classes?

    3. Why do ref classes not support friends?

    4. Why is there no private or protected inheritance for ref classes? (Not such a big deal as, in my experience, it is rarely used; but still...)

    5. My book says value classes are created directly on the stack, but what if you did something like this:
    Code:
    value class Foo
    {
    //...
    }
    int main()
    {
      Foo^ bar = gcnew Foo;
    }
    This compiles, but is it created on the managed heap? The book only says, "...when a new instance of the data type is created, it is allocated either to the stack or CRT heap."

    6. What is the difference between the two different ways of operator overloading in C++/CLI? There is the traditional (or close to) ISO C++ way, as well as static operators; e.g.,
    Code:
    //(almost) ISO C++
    Foo% operator++(int) //also works with Foo& return, but only for value types
    {
    }
    //C++/CLI new syntax
    static Foo^ operator++(const Foo^ lhs)
    {
    }
    If I use the latter version, it only works with handles (^) and if I define both, then only the former is called.

    I could go on with some complaints and design rationale questions, but I'll leave it at that for now
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Most of your questions seem to revolve around the managed constructs. First you have to consider .NET. .NET was designed with other languages in mind. The managed parts of C++/CLI are simply the parts that connect to .NET and potentially other languages. The reason they work different than standard C++ is that they are .NET constructs, not C++ constructs. You still have standard classes to work with when you want those features, and in most cases you can simply wrap the managed stuff if you don't like it. But C++/CLI MUST support both in order to be a .NET programming language.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by JaWiB
    I just got a C++/CLI book a couple days ago, and so far there are a few things that confuse me (the first four aren't really important, just confusing MS decisions).
    That's why I'll always consider C++/CLI a glue language and stick with C# for real .Net development.

    1. First of all, why is there both an enum struct and an enum class if they're both the same.
    Probably to keep the struct/class parallel from C++, or something like that.

    2. Why did MS remove default arguments for ref classes?
    .Net only supports overloading, but not default arguments. It is easy to emulate default arguments using overloading, but MS apparently decided not to secretly generate functions.

    3. Why do ref classes not support friends?
    .Net doesn't support the concept of friends. It uses internal visibility for similar purposes.

    4. Why is there no private or protected inheritance for ref classes? (Not such a big deal as, in my experience, it is rarely used; but still...)
    .Net only supports public inheritance.

    5. My book says value classes are created directly on the stack, but what if you did something like this:
    Code:
      Foo^ bar = gcnew Foo;
    This compiles, but is it created on the managed heap? The book only says, "...when a new instance of the data type is created, it is allocated either to the stack or CRT heap."
    You create a boxed object, i.e. a small managed wrapper containing the value class.

    6. What is the difference between the two different ways of operator overloading in C++/CLI? There is the traditional (or close to) ISO C++ way, as well as static operators;
    One is basically inherited from C++, the other is probably the .Net way of doing things. (Check C# to see how operators are overloaded there. I believe they're static members, mostly.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Ok, so basically everything I dislike about C++/CLI is what MS changed from C++ to be compatible with other .NET languages. That makes sense
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    What I'm getting from that is that you hate .NET. How would you go about providing .NET support to a more standardized C++ then?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    > How would you go about providing .NET support to a more standardized C++ then?

    Change .NET, not C++ (only joking, of course)

    I don't hate .NET, I just dislike how everything in C++/CLI is so similar to C++, but somehow maddeningly different. I guess I'll just get used to it
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Oddities reading binary data
    By KingCandyCorn in forum C Programming
    Replies: 6
    Last Post: 02-15-2009, 05:47 PM
  2. wchar_t, i18n, l10n and other oddities
    By samblack in forum C++ Programming
    Replies: 5
    Last Post: 05-09-2008, 06:57 PM
  3. getch() and other oddities
    By Chaplin27 in forum C++ Programming
    Replies: 14
    Last Post: 02-17-2005, 10:28 AM
  4. Variable Oddities :D
    By Unimatrix139 in forum C Programming
    Replies: 4
    Last Post: 08-01-2002, 03:38 AM