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. about const member function

about const member function

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
3 Posts 3 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.
  • C Offline
    C Offline
    clj19870503
    wrote on last edited by
    #1

    class Text { public: void bad( const string &parm ) const; private: char *_text; }; void Text::bad( const string &parm ) const { _text = parm.c_str(); // error: _text cannot be modified for ( int ix = 0; ix < parm.size(); ++ix ) _text[ix] = parm[ix]; // bad style but not an error } this example show a pointer refer change a data member in the const member function , why?

    S S 2 Replies Last reply
    0
    • C clj19870503

      class Text { public: void bad( const string &parm ) const; private: char *_text; }; void Text::bad( const string &parm ) const { _text = parm.c_str(); // error: _text cannot be modified for ( int ix = 0; ix < parm.size(); ++ix ) _text[ix] = parm[ix]; // bad style but not an error } this example show a pointer refer change a data member in the const member function , why?

      S Offline
      S Offline
      super_ttd
      wrote on last edited by
      #2

      the function is declared as const (void Text::bad(const string& parm) const; ), that means that it is not allowed to modify any members of the Text class. exception is with the mutable members however.

      modified on Sunday, December 09, 2007 11:13:17 AM

      1 Reply Last reply
      0
      • C clj19870503

        class Text { public: void bad( const string &parm ) const; private: char *_text; }; void Text::bad( const string &parm ) const { _text = parm.c_str(); // error: _text cannot be modified for ( int ix = 0; ix < parm.size(); ++ix ) _text[ix] = parm[ix]; // bad style but not an error } this example show a pointer refer change a data member in the const member function , why?

        S Offline
        S Offline
        Steen Krogsgaard
        wrote on last edited by
        #3

        _text is a char*. The const member promises not to change any of the member variables. Your first assignment changes _text to point to a different string. The second assignment (in the loop) does not change _text, but the content of the string that _text points to (btw, I assume that _text has been assigned to a valid string at some point in code not shown, otherwise the second assignment will corrupt your memory). const pointers can point to mutable objects. You may change content of the object, but not the content of the pointer.

        Cheers Steen. "Are you gonna check your makeup when you're done whining?" John Simmons, 05/31/2006 "Of course, the next day it automatically updates, and your quick'n'dirty patches cause the new binaries to segfault all over your linoleum. If only you'd had the source..." Shog, 10/18/2006 "One day I realized that sadness is just another word for not enough coffee" Wally, 10/18/2006

        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