Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Class-constant functions?

Class-constant functions?

Scheduled Pinned Locked Moved C#
csharpc++question
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dominik Reichl
    wrote on last edited by
    #1

    Hello! In C++ we could declare a function like this: BOOL CSomeClass::SomeFunction(BOOL bSomeParameter) **const**; The 'const' meant that the function doesn't change any member variables in the class (CSomeClass). Is there an equivalent keyword in C#? Thanks in advance and best regards, Dominik


    _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

    C T 2 Replies Last reply
    0
    • D Dominik Reichl

      Hello! In C++ we could declare a function like this: BOOL CSomeClass::SomeFunction(BOOL bSomeParameter) **const**; The 'const' meant that the function doesn't change any member variables in the class (CSomeClass). Is there an equivalent keyword in C#? Thanks in advance and best regards, Dominik


      _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Nope - nothing on offer anything remotely like that, sorry. Christian Graus - Microsoft MVP - C++

      V 1 Reply Last reply
      0
      • D Dominik Reichl

        Hello! In C++ we could declare a function like this: BOOL CSomeClass::SomeFunction(BOOL bSomeParameter) **const**; The 'const' meant that the function doesn't change any member variables in the class (CSomeClass). Is there an equivalent keyword in C#? Thanks in advance and best regards, Dominik


        _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

        T Offline
        T Offline
        Tom Larsen
        wrote on last edited by
        #3

        The C# compiler does not provide support for this construct which was dubious in C++ anyway.

        1 Reply Last reply
        0
        • C Christian Graus

          Nope - nothing on offer anything remotely like that, sorry. Christian Graus - Microsoft MVP - C++

          V Offline
          V Offline
          Vikram A Punathambekar
          wrote on last edited by
          #4

          I was thinking about the very same thing a few days back. Any idea *why* it was dropped in C# though? Somebody told me it was dropped because C# doesn't separate the declaration (.h files) from the implementation (.cpp files), but it doesn't sound very convincing. After all, I might want an abstract class' subclasses to be able to use a parameter that is passed to a virtual method, but not modify it. In fact, I found myself in that situation. I ended up re-designing the classes. I'd be grateful if you can provide a convincing answer. Cheers, Vikram.


          I don't know and you don't either. Militant Agnostic

          C 1 Reply Last reply
          0
          • V Vikram A Punathambekar

            I was thinking about the very same thing a few days back. Any idea *why* it was dropped in C# though? Somebody told me it was dropped because C# doesn't separate the declaration (.h files) from the implementation (.cpp files), but it doesn't sound very convincing. After all, I might want an abstract class' subclasses to be able to use a parameter that is passed to a virtual method, but not modify it. In fact, I found myself in that situation. I ended up re-designing the classes. I'd be grateful if you can provide a convincing answer. Cheers, Vikram.


            I don't know and you don't either. Militant Agnostic

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            C# contains very little const support, it's something I've been lobbying for, for some time. I think it's got to do with keeping things simple, something the C# team assure me they will continue to do, although I've presented the opinion that the language often times treats the programmer as an idiot. Christian Graus - Microsoft MVP - C++

            V 1 Reply Last reply
            0
            • C Christian Graus

              C# contains very little const support, it's something I've been lobbying for, for some time. I think it's got to do with keeping things simple, something the C# team assure me they will continue to do, although I've presented the opinion that the language often times treats the programmer as an idiot. Christian Graus - Microsoft MVP - C++

              V Offline
              V Offline
              Vikram A Punathambekar
              wrote on last edited by
              #6

              Ahem, so it can't be done? No clever hacks? I'm starting to miss C++. :~ I want my class' subclasses to be unable to modify a parameter passed to a virtual method they derive from my class. Will my subclasses be free to run amok? :sigh: Cheers, Vikram.


              I don't know and you don't either. Militant Agnostic

              T 1 Reply Last reply
              0
              • V Vikram A Punathambekar

                Ahem, so it can't be done? No clever hacks? I'm starting to miss C++. :~ I want my class' subclasses to be unable to modify a parameter passed to a virtual method they derive from my class. Will my subclasses be free to run amok? :sigh: Cheers, Vikram.


                I don't know and you don't either. Militant Agnostic

                T Offline
                T Offline
                Tom Larsen
                wrote on last edited by
                #7

                The problem is that in the C# treats const much more "seriously" than C++. In fact a problem common in C++ code is accidently casting away something const by shifting to a reference. Look at the behavior of the keyword sealed. `sealed` can protect a lot of things from having the inheritor from overriding, modifying, etc base class behavior.

                V 1 Reply Last reply
                0
                • T Tom Larsen

                  The problem is that in the C# treats const much more "seriously" than C++. In fact a problem common in C++ code is accidently casting away something const by shifting to a reference. Look at the behavior of the keyword sealed. `sealed` can protect a lot of things from having the inheritor from overriding, modifying, etc base class behavior.

                  V Offline
                  V Offline
                  Vikram A Punathambekar
                  wrote on last edited by
                  #8

                  Tom Larsen wrote:

                  The problem is that in the C# treats const much more "seriously" than C++.

                  I'm still new to C#, but I don't understand the basis of your statement. Could you please explain in greater detail?

                  Tom Larsen wrote:

                  sealed can protect a lot of things from having the inheritor from overriding, modifying, etc base class behavior.

                  Yes, but unfortunately my base method was abstract, so there's no question of sealing it. :( Cheers, Vikram.


                  I don't know and you don't either. Militant Agnostic

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups