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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:15:48+00:00 2026-05-17T19:15:48+00:00

Let me say we have a simple programming task. But for the sake of

  • 0

Let me say we have a simple programming task. But for the sake of clarity I start with few code samples.
First of all we written a some kind of data container class but for the purposes of task no matter what the class is. We just need it to behave const-correct.

class DataComponent {
public:
    const std::string& getCaption() const {
        return caption;
    }

    void setCaption(const std::string& s) {
        caption = s;
    }

private:
    std::string caption;
};

Then let us assume we’ve got a generic class that behaves like facade over arbitrary incapsulated class instance. Say we overloaded member access operator (->).

template <typename T> class Component {
public:
    Component() { instance = new T(); }

    ...

    const T* operator-> () const {
        return instance;
    }

    T* operator-> () {
        // but there might be additional magic
        return instance;
    }

private:
    T *instance;
};

At this point I should say how I want this to work:

  • if we’re calling non-const member functions of underlying class through member access operator (component->setCaption("foo")) compilier treats non-const T* operator-> () as the best choice.
  • otherwise if we are trying to call const member functions of underlying class same way (component->getCaption()) compiliers selects const T* operator-> () const on the other hand.

This code sample above won’t work this way so I’m curious about possibility to give compiler a behavior like that I have mentioned. Any propositions.


EDIT: Let our member access operator overloaded this way:

    const T* operator-> () const { return instance; }

    T* operator-> () {
        cout << "something going change" << endl;
        return instance;
    }

And let us have a variable Component<DataComponent> c somewhere. Then on the call to c->getCaption() stdout should remain silent but on the call to c->setCaption("foo") stdout should warn us that something is going to change. VS 2010 compilier makes stdout warn us on each of these calls.

I understand that such semantics suppose that c behaves as const and non-const at the same time. But curiousity is still in my mind.

  • 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-17T19:15:48+00:00Added an answer on May 17, 2026 at 7:15 pm

    Whether a const or non-const member is invoked is determined purely by the constness of the object on which it is invoked, not by some subsequent operation. That determination is made before any consideration of the particular method you’re invoking in DataComponent. You could still hack up the required functionality less directly using proxy object around DataComponent, with both const and non-const forwarding getCaption()s.

    EDIT: details as requested (off the top of my head). You’ll need to forward declare some of this stuff – I didn’t bother as it makes it even more confusing. Do chip in with any concerns / feedback. Note that this basically assumes you can’t / don’t want to modify Component for some reason, but it’s not a generic templated solution that can simply be wrapped around any arbitrary type – it’s very heavily coupled and has a high maintenance burden.

    // know they can't call a non-const operation on T, so this is ok...
    const T* Component::operator->() const { return instance; }
    
    // they might invoke a non-const operation on T, so...
    DataComponent::Proxy Component::operator->() { return DataComponent.getProxy(*this); }
    

    in class DataComponent:

    struct Proxy
    {
        Component& c_;
        DataComponent& d_;
    
        Proxy(Component& c, DataComponent& d) : c_(c), d_(d) { }
    
        const std::string& get_caption() const { return d_.get_caption(); }
    
        void set_caption(const std::string& s)
        {
            c_.on_pre_mutator(d_);
            d_.set_caption(s);
            c_.on_post_mutator(d_);
        }
    };
    

    then

    DataComponent::Proxy DataComponent::getProxy(Component& c) { return Proxy(c, *this); }
    

    So, this means somewhere you have to hand-code forwarding functions. It’s a pain, but if you’re doing this for debugging or testing it’s not unreasonable. If you’re doing this so you can add a lock or something, then there are probably better alternatives.

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

Sidebar

Related Questions

Let's say I'm programming in Java or Python or C++ for a simple problem,
Let's say you have to [write from scratch, rewrite, refactor] a sample program illustrating
Quick background: I'm programming in PHP, I have a domain model with a separate
This should be a simple question, but I just can't recall the relevant API.
Does anybody know, how to get a list of leaf nodes in Prolog? Let's
Another newbie (Common) LISP question: Basically in most programming languages there's a mean for
When programming in PHP I always try to create meaningful 'models' (classes) that correspond
I recently asked a question about functional programming, and received (good!) answers that prompted
Hello Again my fellow programmers out there, I'm designing and programming from scratch a
I'm relatively well versed in designing websites. I mostly go for LAMP where I

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.