The futility of const ref objects

Most C++ programmers would have trained themselves to use const objects and const member functions whenever and wherever possible, and rightly so. But, if you try to declare a const member function for a ref class, you’ll get error C3842 – member functions of managed types cannot be const. So, while you can declare a ref object to be const, you’d serve no purpose in doing so, because you won’t be able to invoke any methods on it, except by using const_cast to remove the const-ness of the object. I believe that the idea behind this was to be compatible with other CLI languages like C# which do not support const member functions. I am told that a future release of the compiler would allow managed member functions to be declared const but that this is not a high priority target as of now. So, this is one more difference when using C++ to do CLI programming compared to doing native programming.


3 thoughts on “The futility of const ref objects

  1. Nish:

    What do you think about something like

    #define CONST

    void SomeClass::SomeMethod() CONST

    so that we can at least pretend to be const correct (no compiler checking).

    Then when the day comes, if ever, we can change this to

    #define CONST const

    Const-correctness was so hard at the beginning, but now it’s hard to live without it.

    BTW, are you going to be making a C++/CLI version of your book?


  2. This is sad. Const correctness is such an important quality control tool, and it’s very hard to add it later into a language, because there will be millions of lines of code out there not using const. It’s a bad design in the .NET framework itself that it doesn’t support const correctness. It’s definitely a few steps backwards.

  3. hi,
    i want to MDI application with same as document/view architecture in C++/CLI.
    Its to urgent in my new project.
    Please help me.
    take care

    viral shah

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s