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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:39:10+00:00 2026-05-23T08:39:10+00:00

I have a couple of classes one of which keeps reference to the object

  • 0

I have a couple of classes one of which keeps reference to the object of other:

class Inner {};

class Outer {
    Inner & in;

public:
    Outer(Inner & in) : in(in) {}
};

What if I have to create Outer object from const reference to Inner? Am I have to write specific class, say OuterConst, for this?

UPD: Any neat solutions using templates? In order to avoid duplication of code in OuterConst class.

UPD2: when Inner comes without const it should be modifiable (so I can’t just add const to Inner member in current implementation of Outer).

  • 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-23T08:39:11+00:00Added an answer on May 23, 2026 at 8:39 am

    You could store a const reference, and use const_cast to cast the const away when needed. If you add some runtime checking, it’s also safe.

    class Inner { /* ... */ };
    
    class Outer {
        Inner const & in;
        bool const inIsConst;
    
        Inner & inMutable()
        {
            if (inIsConst)
                throw std::logic_error("in is const.");
            else
                return const_cast<Inner &>(in);
        }
    
    public:
        Outer(Inner const & in) : in(in), inIsConst(true) {}
        Outer(Inner & in) : in(in), inIsConst(false) {}
    
        // always safe
        void Foo() { std::cout << in.getFoo(); }
    
        // will throw if *this was constructed with a const Inner
        void Bar() { inMutable().setFoo(in.getFoo() + 1); }
    };
    

    And of course you can always split this into two classes: one that cannot change in, and another that can.

    To do this, you could either derive the class that can change in from the class that cannot. Or you could derive both from a common base class which has the common functions as protected members, and make the appropriate ones public with using Base::Function in the derived classes.

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

Sidebar

Related Questions

I have this simple application with a couple of classes which are all related.
I have a re-occurring design problem with certain classes which require one-off initialization with
I have a couple of classes in which I need more control over member
I have a couple of elements, one of which may have any number of
I have an assignment and we have a couple of classes given, one of
I have a couple of classes BitMask & BitMaskLarge that both implement an interface
I have a couple AsyncTask classes in my main activity, each of which fetches
When starting my application, I have a couple of classes which are required to
I have two classes, one has a lot of properties and the other has
ok I have couple of .NET classes that I want to use in VBA.

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.