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

  • SEARCH
  • Home
  • 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 6690759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:40:26+00:00 2026-05-26T05:40:26+00:00

The const qualifier on a method supposed to protect the data member from overwriting

  • 0

The const qualifier on a method supposed to protect the data member from overwriting by mistake. If you have a data member which is a pointer then only the pointer is defended, the pointed value is not. Is it a flaw in the design of C++ or is there something fundamental thing it serves?

Here is a code which demonstrates the situation. Before reporting irrelevant bugs and style problems, please consider that its sole purpose is to demonstrate the above situation and to be short and straightforward in that.

#include <cstdio>
#include <cstdlib>
#include <cstring>

class Cat
{
public:
        Cat(char const *name)
        : _name(strdup(name))
        { }
        ~Cat(){ free(_name); }
        void SetName(char const *name)
        {
                free(_name);
                _name = strdup(name);
        }
        char const* GetName() const
        {
                _name[0] = 'P';
                return _name;
        }
private:
        char *_name;
};

int main()
{
        Cat c("lost+found");
        c.SetName("Molly");
        printf("%s\n",c.GetName());
        return 0;
}

Compilation went without warnings and errors with the following command:

g++ -W -Wall -Wextra -pedantic -Os pmc.cpp -o pmc

The output of the resulting program was Polly.

UPDATE Using pedantic char const * instead of traditional const char *

  • 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. Editorial Team
    Editorial Team
    2026-05-26T05:40:27+00:00Added an answer on May 26, 2026 at 5:40 am

    The fundamental thing served here is simply clarity. The pointer is a member of the class, its pointee is not: It is neither automatically destructed when the class is nor automatically constructed. It might be a wholly different object, so it should not necessarily be const when the class becomes const.

    You are desiring something like std::string here, that is, additional semantics for the pointer – for example the guarantee that its contents will not change outside of your class.

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

Sidebar

Related Questions

I have a method void foo(list<shared_ptr<Base>>& myList); Which I'm trying to call with a
I am trying to specialize some utility code on const member functions, but have
Say I have this example: char const * const foo( ){ /* which is
A pointer to non-const data can be implicitly converted to a pointer to const
Possible Duplicate: Why isn't the const qualifier working on pointer members on const objects?
I have a simple const method that wants to generate a random number int
why do I get a discard qualifiers error: customExc.cpp: In member function ‘virtual const
I have a private const int defined in a C# class. I want compilation
The const modifier in C++ before star means that using this pointer the value
From Herb Sutter's GotW #6 Return-by-value should normally be const for non-builtin return types.

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.