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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:01:42+00:00 2026-06-17T20:01:42+00:00

Consider the following pattern: class Child { public: char Foo; Child(char foo) { Foo

  • 0

Consider the following pattern:

class Child
{
public:
    char Foo;

    Child(char foo)
    {
        Foo = foo;
    }
};

class Parent
{
public:
    Child c;

    Parent() : c('A') { }

    const Child& GetChild() const
    {
        return c;
    }
};

// Scenario 1: Works, but useless
int main()
{
    Parent p = Parent();
    Child c = p.GetChild();
    c.Foo = 'B';
    cout << p.c.Foo << endl; // A, wrong
    cout << c.Foo << endl; // B, good
    system("PAUSE");
}

// Scenario 2: Doesn't compile, of course
int main()
{
    Parent p = Parent();
    Child& c = p.GetChild(); // Error
    c.Foo = 'B';
    cout << p.c.Foo << endl; // A, good
    cout << c.Foo << endl; // B, good
    system("PAUSE");
}

The specification is the following:

  • The getter must be defined as const (because it doesn’t modify Parent)
  • The reference given by the getter must modify the underlying value

The problem is that:

  • C++ requires the return value of the getter to be const if the getter itself is const (why?)
  • C++ forbids assigning a const value to a reference (logically)

It is very easy to accomplish this using pointers (make the accessor return Child*), but there seems to be a consensus (and rightfully so) that references are advisable, considering they hide the complexity of pointers.

Is there any way to do it? If not, I’ll just revert to pointers.

  • 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-17T20:01:44+00:00Added an answer on June 17, 2026 at 8:01 pm

    If the returned reference wouldn’t be const, the caller could modify the object even if it was const in its own context:

    const Parent p = ...
    Child & child = p.GetChild(); // valid const call, invalid return type
    

    However, this is only the problem when you are trying to return a member variable which is part of the class itself (which itself is not a pointer). So as you already suggested, making the Child a pointer would be OK. But returning a non-const pointer to a non-pointer Child would result in the same problem…

    To illustrate the problem better, consider this memory layout illustration:

    Parent:  [Child]
    
    Child:   [char ]
    

    So a parent contains a Child object and no more, so modifying your Parent instance modifies a Child instance, while modifying Parent::c modifies Parent itself.

    So you can’t return a pointer or a reference to a non-const object (which the this pointer points at in a const member function):

    Child& GetChild() const
    {
        return c;    // compiler will complain here
    }
    

    Would be equal to:

    Child& GetChild() const
    {
        return this->c;   // compiler will complain here
    }
    

    where this is of type const Parent *, which means that this->c is of type const Child &. Returning Child & violates the const-ness.

    The getter itself doesn’t modify the object, but it allows to circumvent the const-ness within the caller’s code, as seen in the code above.

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

Sidebar

Related Questions

Consider the following class. class mapping_items { public: mapping_items(){} void add(const mapping_item* item) {
Consider following 2 programs giving same error First calss: public class Testing { Testing
Please consider the following snippet from an implementation of the Interpreter pattern: public override
Please consider the following code: type TFoo1 = class public procedure DoSomething1; end; TFoo2
Consider the following code parts: this is Timing.h: class Timing { public: //creates a
Consider the following code: unordered_set<T> S = ...; for (const auto& x : S)
consider following: 1st APPROACH: public void f3() { f2(); f1(); } and this ...
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider the following scenario: Page written in classic ASP or PHP, which is rendering
Consider the following Spring Service class. The spring scope defined is Singleton. The two

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.