Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 93097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:17:27+00:00 2026-05-10T23:17:27+00:00

class String { private: char* rep; public: String (const char*); void toUpper() const; };

  • 0
class String {      private:         char* rep;      public:         String (const char*);         void toUpper() const; };   String :: String (const char* s) {     rep = new char [strlen(s)+1];     strcpy (rep, s); }   void String :: toUpper () const {     for (int i = 0; rep [i]; i++)     rep[i] = toupper(rep[i]); }   int main () {     const String lower ('lower');     lower.toUpper();      cout << lower << endl;     return 0; } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:17:28+00:00Added an answer on May 10, 2026 at 11:17 pm

    A const member function, is a member function that does not mutate its member variables.

    const on a member function does not imply const char *. Which would mean that you can’t change the data in the address the pointer holds.

    Your example does not mutate the member variables themselves.

    A const on a member function, will ensure that you treat all of your member variables as const.

    That means if you have:

    int x; char c; char *p; 

    Then you will have:

    const int x; const char c; char * const p; //<-- means you cannot change what p points to, but you can change the data p points to 

    There are 2 types of const pointers. A const member function uses the one I’ve listed above.


    A way to get the error you want:

    Try changing:

    char * rep; 

    to:

    char rep[1024]; 

    And remove this line:

    rep = new char [strlen(s)+1]; 

    It will throw the error you are expecting (can’t modify members because of const keyword)

    Because there is only 1 type of const array. And that means you cannot modify any of its data.


    Now the whole system is actually broken with the following example:

    class String {      private:         char rep2[1024];         char* rep;   ...    String :: String (const char* s)  {     rep = rep2;     strcpy (rep, s);   } 

    So the lesson to learn here is that the const keyword on member functions does not ensure that your object will not change at all.

    It only ensures that each member variable will be treated as const. And for pointers, there is a big diff between const char * and char * const.

    Most of the time a const member function will mean that the member function will not modify the object itself, but this is not always the case, as the above example shows.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 215k
  • Answers 215k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer n is your list item, so something like this should… May 12, 2026 at 10:54 pm
  • Editorial Team
    Editorial Team added an answer The keyword of what you are seeking seems to be… May 12, 2026 at 10:54 pm
  • Editorial Team
    Editorial Team added an answer There seem to be two problems: keeping the USB powered… May 12, 2026 at 10:54 pm

Related Questions

I'm learning cpp and In my last assignment I am rewriting the std::string class.
I just start playing with GWT I'm having a really hard time to make
I'm trying to input a form of data validation in which when a user
I have a template class like below. template<int S> class A { private: char

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.