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 8170605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:10:44+00:00 2026-06-06T21:10:44+00:00

Look at this very simple piece of code: struct A { char* s; };

  • 0

Look at this very simple piece of code:

struct A { char* s; };

class B
{
    A* a;

    public: B(const char* s) : a(new A()) {
        int len = strlen(s);
        a->s = new char[len + 1];
        memcpy(a->s, s, len + 1);
    }

    ~B() { delete [] a->s; delete a; }

    const char* c_str() const { return a->s; }

    const B& to_upper() const {
        char* x = a->s;
        int len = strlen(x);
        for (int i = 0; i < len; i++)
        {
            char k = x[i];
            if (k >= 'a' && k <= 'z')
                x[i] -= 32;
        }
        a->say_hi();
        return *this;
    }
};

int main() {
    B b = "hola mundo";
    printf("%s\n", b.to_upper().c_str());
}

It works!! My question is… why?

The to_upper() method is const and modifies the value pointee by “a”. Ok, I am not able to do something like “a = nullptr;” because the compiler says: “You are trying to modify a read-only object”; but it lets me modifying the underlying values. Is this behavior correct? Shouldn’t the “a” type be converted to “const A*” in the same way that the type of “this” is converted to “const B*” in the const method?

Thanks!

  • 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-06-06T21:10:46+00:00Added an answer on June 6, 2026 at 9:10 pm

    The constness of a method translates to the constness of the *this object, meaning that inside to_upper the this pointer has type const B *. That’s all there is to it. No more, no less. The effect of this is not different from what you would see in C language, for example. It makes pointer this->a const, but it doesn’t affect the pointee.

    In fact it is up to you to decide whether the constness of B propagates to the A object pointed by this->a. The language gives you full freedom in making this decision. It is called “conceptual constness” (as opposed to “physical constness” or “logical constness”). The compiler observes and enforces only logical constness, while the purpose of keyword const in OOP goes far beyond that: it allows you to implement the idea of conceptual constness in your design.

    If the A object is considered an integral part of B, then the constness of B should also mean constness of A. But this is something you have to observe and enforce manually (or some smart pointer class can help you with this).

    If A object is an independent object which just happens to be merely referenced from B, then constness of B should not necessarily imply constness of A.

    The compiler does not impose any decisions on you with respect to this, since compiler has no idea what object relationship you are trying to implement. In your design, the way I see it, the A object is actually an integral part of B, which means that you weren’t supposed to declare your to_upper as const. It is a modifying function. It changes what is perceived by the user as the value of B. By declaring to_upper as const you are essentially “lying” to the user.

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

Sidebar

Related Questions

I have this very simple wrapper template: template<class T> struct wrapper { inline operator
Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
Look at this code: class MyClass(): # Why does this give me NameError: name
Look at this simple page: <!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd> <html>
MySQL Version 5.0.67 Take a look at this very simple table and tell me
Take this very simple form for example: class SearchForm(Form): q = forms.CharField(label='search') This gets
I have very long integer sequences that look like this (arbitrary length!): 0000000001110002220033333 Now
:) This might look to be a very long question to you I understand,
Look ath this method of ThreadPoolExcecutor: public void execute(Runnable command) { ... if (runState
look at this code, <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8 /> <script> function change() {

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.