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. one doubt abt memberfunctions.....

one doubt abt memberfunctions.....

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 7 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.
  • N Offline
    N Offline
    namaskaaram
    wrote on last edited by
    #1

    tis a crap question but i still would want to clear em..... say that i have class say:

    class xyz
    {
    int a;
    public:
    int return_value(void);
    };

    int xyz::return_value(void)
    {
    return(a);
    }

    as the above function is does not manipulate the data of the class,we can declare it also as ......

    class xyz
    {
    int a;
    public:
    int return_value(void) const;
    };

    int xyz::return_value(void) const
    {
    return(a);
    }

    right?.....what i want to know iz iz there any other advantage of writing a 'const' after function that does not manipulate the data?..... cheerz.....:-D "faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13

    R D S 3 Replies Last reply
    0
    • N namaskaaram

      tis a crap question but i still would want to clear em..... say that i have class say:

      class xyz
      {
      int a;
      public:
      int return_value(void);
      };

      int xyz::return_value(void)
      {
      return(a);
      }

      as the above function is does not manipulate the data of the class,we can declare it also as ......

      class xyz
      {
      int a;
      public:
      int return_value(void) const;
      };

      int xyz::return_value(void) const
      {
      return(a);
      }

      right?.....what i want to know iz iz there any other advantage of writing a 'const' after function that does not manipulate the data?..... cheerz.....:-D "faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13

      R Offline
      R Offline
      RChin
      wrote on last edited by
      #2

      AFAIK, the 'advantage' of using a constant function is to protect member variable integrity from being modified. Obviously you would not use it everywhere, but for areas of your class that has functions that you know should be read-only, then this sort of safe-guards the class from unscrupulously changing data it shouldn't.


      I Dream of Absolute Zero

      1 Reply Last reply
      0
      • N namaskaaram

        tis a crap question but i still would want to clear em..... say that i have class say:

        class xyz
        {
        int a;
        public:
        int return_value(void);
        };

        int xyz::return_value(void)
        {
        return(a);
        }

        as the above function is does not manipulate the data of the class,we can declare it also as ......

        class xyz
        {
        int a;
        public:
        int return_value(void) const;
        };

        int xyz::return_value(void) const
        {
        return(a);
        }

        right?.....what i want to know iz iz there any other advantage of writing a 'const' after function that does not manipulate the data?..... cheerz.....:-D "faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        It simply indicates to the caller that the method is "read-only" and does not modify the object for which it is called. It can also serve as a reminder to anyone that changes the class.


        "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

        T 1 Reply Last reply
        0
        • D David Crow

          It simply indicates to the caller that the method is "read-only" and does not modify the object for which it is called. It can also serve as a reminder to anyone that changes the class.


          "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          DavidCrow wrote: It simply indicates to the caller that the method is "read-only" and does not modify the object for which it is called. It can also serve as a reminder to anyone that changes the class. ...and prevent any modification on the object from within the function...


          TOXCCT >>> GEII power
          [toxcct][VisualCalc]

          D V 2 Replies Last reply
          0
          • T toxcct

            DavidCrow wrote: It simply indicates to the caller that the method is "read-only" and does not modify the object for which it is called. It can also serve as a reminder to anyone that changes the class. ...and prevent any modification on the object from within the function...


            TOXCCT >>> GEII power
            [toxcct][VisualCalc]

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Yes, that's what "does not modify the object for which it is called" means.


            "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

            1 Reply Last reply
            0
            • N namaskaaram

              tis a crap question but i still would want to clear em..... say that i have class say:

              class xyz
              {
              int a;
              public:
              int return_value(void);
              };

              int xyz::return_value(void)
              {
              return(a);
              }

              as the above function is does not manipulate the data of the class,we can declare it also as ......

              class xyz
              {
              int a;
              public:
              int return_value(void) const;
              };

              int xyz::return_value(void) const
              {
              return(a);
              }

              right?.....what i want to know iz iz there any other advantage of writing a 'const' after function that does not manipulate the data?..... cheerz.....:-D "faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13

              S Offline
              S Offline
              S Senthil Kumar
              wrote on last edited by
              #6

              The compiler will complain if you try to modify data from within the function. Also, if you declare your member functions const, that'll enable callers to call them from const functions.. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

              J 1 Reply Last reply
              0
              • S S Senthil Kumar

                The compiler will complain if you try to modify data from within the function. Also, if you declare your member functions const, that'll enable callers to call them from const functions.. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                J Offline
                J Offline
                James R Twine
                wrote on last edited by
                #7

                S. Senthil Kumar wrote: Also, if you declare your member functions const, that'll enable callers to call them from const functions.    ...to call them from const objects. :)    Peace! -=- James


                If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                DeleteFXPFiles & CheckFavorites (Please rate this post!)

                S 1 Reply Last reply
                0
                • T toxcct

                  DavidCrow wrote: It simply indicates to the caller that the method is "read-only" and does not modify the object for which it is called. It can also serve as a reminder to anyone that changes the class. ...and prevent any modification on the object from within the function...


                  TOXCCT >>> GEII power
                  [toxcct][VisualCalc]

                  V Offline
                  V Offline
                  vladfein
                  wrote on last edited by
                  #8

                  It doesn't "prevent" such modifications. Just makes it more difficult and explicit (like using const_cast). ----------------------------- Get trial copy of comment generating tool CommentMakerPro, a collection of convenience and productivity tools for Microsoft Visual Studio .NET FeinStuff, and std::string and std::string containers viewer FeinEvaluatorPro at www.FeinSoftware.com

                  V 1 Reply Last reply
                  0
                  • J James R Twine

                    S. Senthil Kumar wrote: Also, if you declare your member functions const, that'll enable callers to call them from const functions.    ...to call them from const objects. :)    Peace! -=- James


                    If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                    Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                    DeleteFXPFiles & CheckFavorites (Please rate this post!)

                    S Offline
                    S Offline
                    S Senthil Kumar
                    wrote on last edited by
                    #9

                    That's right :) Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                    1 Reply Last reply
                    0
                    • V vladfein

                      It doesn't "prevent" such modifications. Just makes it more difficult and explicit (like using const_cast). ----------------------------- Get trial copy of comment generating tool CommentMakerPro, a collection of convenience and productivity tools for Microsoft Visual Studio .NET FeinStuff, and std::string and std::string containers viewer FeinEvaluatorPro at www.FeinSoftware.com

                      V Offline
                      V Offline
                      vladfein
                      wrote on last edited by
                      #10

                      I would REALLY appreciate if whoever voted my post down (not that I care about the "score") explained their position. Vlad. ----------------------------- Get trial copy of comment generating tool CommentMakerPro, a collection of convenience and productivity tools for Microsoft Visual Studio .NET FeinStuff, and std::string and std::string containers viewer FeinEvaluatorPro at www.FeinSoftware.com

                      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