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 / C++ / MFC
  4. const help

const help

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++graphicshelplounge
13 Posts 5 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.
  • R ROK_RShadow

    Hello. This is a general C++ question. Can somebody please shed some light on the const modifier? I have been programming for about a year.. and I never use const because I really don't understand it... so I am trying to gain some ground, and I just can't understand this. Please look at the class declaration below, and can somebody explain to me why the functions draw(), and getx() allow me to modify the private data members. they are declared const, so they shouldn't allow me to modify those variables.. but they do. according to MSDN help library, you declare a member function const by placing the keyword after the parameter list.. this is what I did.. but it still lets me modify them. I know if I declare the variables const it will work.. but shouldn't I be able to declare a function const so the compiler won't let me modify? class cRectangle { public: // constructors cRectangle(); cRectangle(int nL, int nW); // Destructor ~cRectangle(); // public accesors void SetLength(int nL); void SetWidth(int nW); int GetLength() const { return *nLength;} int GetWidth() const { return *nWidth;} // public methods void Draw() const; private: // Data Members int* nLength; // pointers because they will be stored on the heap int* nWidth; }; // implementation file // striped but draw() function // Drawing functions void cRectangle::Draw() const { // try and change a variable *nWidth = 42; // why its this allowed???? // function is const? should not let me change // the object. } If anybody could explain this to me. It would be greatly appreciated. Thank you.

    T Offline
    T Offline
    Taka Muraoka
    wrote on last edited by
    #4

    const prevents the method from changing the value of any data members. In this case, it's the pointers. You're changing the value of what is being pointed to.


    he he he. I like it in the kitchen! - Marc Clifton (on taking the heat when being flamed) NEW: Awasu v0.7[^]: A free RSS reader with support for Code Project.

    M 1 Reply Last reply
    0
    • C Chris Losinger

      because nWidth is a pointer to an int, not an int. your Draw code is changing the value that nWidth points to, not nWidth itself. and it will probably give you an access violation, since nWidth isn't initialized. if you change the Draw code to "nWidth = 42", it will not compile. -c


      A | B - it's not a choice.

      ThumbNailer

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

      Damn - looks like you can type faster than me. :-) Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
      C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
      It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003

      C 1 Reply Last reply
      0
      • C Christian Graus

        Damn - looks like you can type faster than me. :-) Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
        C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
        It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #6

        i even made a quick project to test it out, just to be sure! :) -c


        A | B - it's not a choice.

        ThumbNailer

        C 1 Reply Last reply
        0
        • C Chris Losinger

          i even made a quick project to test it out, just to be sure! :) -c


          A | B - it's not a choice.

          ThumbNailer

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

          You're a machine, I stand in awe.... All I did was type. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
          C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
          It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003

          1 Reply Last reply
          0
          • C Christian Graus

            You're not changing nWidth, you're changing the value stored in it. Why is nWidth a pointer ? When your function returns, it will still point to the same place. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
            C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
            It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003

            R Offline
            R Offline
            ROK_RShadow
            wrote on last edited by
            #8

            ok. I understand kind of.. even if a function is constant, it can still modify what a pointer points to.

            C 1 Reply Last reply
            0
            • R ROK_RShadow

              ok. I understand kind of.. even if a function is constant, it can still modify what a pointer points to.

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

              MAybe this will help - if you create a pointer you can do this: const int * p_i, or this: int * const p_i the second syntax makes the value const, the first the memory address. A const function only enforces the first idea. const int * const p_i is also valid syntax for both. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
              C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
              It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003

              R 1 Reply Last reply
              0
              • C Christian Graus

                MAybe this will help - if you create a pointer you can do this: const int * p_i, or this: int * const p_i the second syntax makes the value const, the first the memory address. A const function only enforces the first idea. const int * const p_i is also valid syntax for both. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
                C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
                It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003

                R Offline
                R Offline
                ROK_RShadow
                wrote on last edited by
                #10

                Thank you. I understand what is going on. I don't want to keep asking stupid questions.. but if that is the case, then how would you solve this problem. suppose you have a class, and this class has some pointers to different objects.. now some functions of the class need to be able to modify the data of what the pointer points too, but you also have some functions that just need to retrieve the data, and you want to make sure that those functions do not modify the pointed to data.

                C 1 Reply Last reply
                0
                • R ROK_RShadow

                  Thank you. I understand what is going on. I don't want to keep asking stupid questions.. but if that is the case, then how would you solve this problem. suppose you have a class, and this class has some pointers to different objects.. now some functions of the class need to be able to modify the data of what the pointer points too, but you also have some functions that just need to retrieve the data, and you want to make sure that those functions do not modify the pointed to data.

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

                  How about int * const pTemp = m_pInt; so in the function you always access a const pointer ? I can't think of an iron clad solution, i.e. one that does not rely on the functions implimenter 'playing by the rules'. Unless I have external clients, I don't use const functions much at all, and if you declare it const and don't modify anything, then the fact that you could is immaterial, you made a contract with your classes clients and you kept it. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
                  C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
                  It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003

                  R 1 Reply Last reply
                  0
                  • C Christian Graus

                    How about int * const pTemp = m_pInt; so in the function you always access a const pointer ? I can't think of an iron clad solution, i.e. one that does not rely on the functions implimenter 'playing by the rules'. Unless I have external clients, I don't use const functions much at all, and if you declare it const and don't modify anything, then the fact that you could is immaterial, you made a contract with your classes clients and you kept it. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
                    C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
                    It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003

                    R Offline
                    R Offline
                    ROK_RShadow
                    wrote on last edited by
                    #12

                    Thank you Christian. That was the answer I came up with, but with my limited experince, I just wanted to make sure.

                    1 Reply Last reply
                    0
                    • T Taka Muraoka

                      const prevents the method from changing the value of any data members. In this case, it's the pointers. You're changing the value of what is being pointed to.


                      he he he. I like it in the kitchen! - Marc Clifton (on taking the heat when being flamed) NEW: Awasu v0.7[^]: A free RSS reader with support for Code Project.

                      M Offline
                      M Offline
                      Mike Nordell
                      wrote on last edited by
                      #13

                      Taka Muraoka wrote: const prevents the method from changing the value of any data members Not quite. const is a contract, a promise, to not change the logical state of an object, when applied to a member function. Imagine a class that has a backing store (e.g. a database) for the data it can present to the user. Any GetFoo() should probably be a const member function. Later on you realize "Hey, this stuff is waaay to slow. I need a cache!". You implement such a cache. But, that would change the objects state, right? No. This is where the mutable keyword come in play. A const member function may modify mutable qualified data - even that the "contract" you have with the clients of that code promises that it may not change the objects logical/observable state. This is just scraping the top of the iceberg. Const correctness is an artform in itself, and depending on scope it can mean a whole lot of things. For further information, I think Marshall Cline's C++ FAQ Lite could be a good starting point.

                      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