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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:48:48+00:00 2026-05-13T12:48:48+00:00

Edit1: I realize this is hard to understand this question without having an insight

  • 0

Edit1: I realize this is hard to understand this question without having an insight of what I’m trying to do. The class A is not complete but it essentially stand for a C-array “proxy” (or “viewer” or “sampler”). One interesting usage is too present a C-array as a 2d grid (the relevant function are not shown here). The property of this class are the following:

  • it should not own the data – no deep copyy
  • it should be copyable/assignable
  • it should be lightweight (
  • it should preserve constness (I’m having trouble with this one)

Please do not question the purpose or the design: they are the hypothesis of the question.

First some code:

class A
{
private:
    float* m_pt;
public:
    A(float* pt)
        :m_pt(pt)
    {}
    const float* get() const
    {
        return m_pt;
    }
    void set(float pt)
    {
        *m_pt = pt;
    }
};

void gfi()
{
    float value = 1.0f;
    const A ac(&value);
    std::cout<<(*ac.get())<<std::endl;
    A a = ac;
    a.set(2.0f);
    std::cout<<(*ac.get())<<std::endl;
}

Calling “gfi” generate the following output:

1
2

Assigning a with ac is a cheap way to shortcut the constness of ac.
Is there a better way to protect the value which m_pt point at?

Note that I DO want my class to be copyable/assignable, I just don’t want it to loose its constness in the process.

Edit0: I also DO want to have a pointer in there, and no deep copy please (let say the pointer can be a gigantic array).

Edit2: thanks to the answers, I came to the conclusion that a “const constructor” would be a useful thing to have (at least in this context). I looked it up and of course I’m not the same one who reached this conclusion. Here’s an interesting discussion:
http://www.rhinocerus.net/forum/language-c-moderated/569757-const-constructor.html

Edit3: Finally got something which I’m happy with. Thanks for your help. Further feedback is more than welcome

template<typename T>
class proxy
{
public:
    typedef T elem_t;
    typedef typename boost::remove_const<T>::type elem_unconst_t;
    typedef typename boost::add_const<T>::type elem_const_t;
public:
    elem_t* m_data;
public:
    proxy(elem_t* data = 0)
        :m_data(data)
    {}
    operator proxy<elem_const_t>()
    {
        return proxy<elem_const_t>(m_data);
    }
}; // end of class proxy

void test()
{
    int i = 3;
    proxy<int> a(&i);
    proxy<int> b(&i);
    proxy<const int> ac(&i);
    proxy<const int> bc(&i);
    proxy<const int> cc = a;
    a=b;
    ac=bc;
    ac=a;
    //a=ac; // error C2679: binary '=' : no operator found which takes a right-hand operand of type...
    //ac.m_data[0]=2; // error C3892: 'ac' : you cannot assign to a variable that is const
    a.m_data[0]=2;
}
  • 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-13T12:48:49+00:00Added an answer on May 13, 2026 at 12:48 pm

    EDIT: considering this question some more, I think you are misinterpreting the effect of const-correctness on member pointers. Consider the following surprising example:

    //--------------------------------------------------------------------------------
    class CNotSoConstPointer
     {
     float *mp_value;
    
     public:
       CNotSoConstPointer(float *ip_value) : mp_value(ip_value) {}
    
       void ModifyWithConst(float i_value) const
         {
         mp_value[0] = i_value;
         }
    
       float GetValue() const
         {
         return mp_value[0];
         }
     };
    
    //--------------------------------------------------------------------------------
    int _tmain(int argc, _TCHAR* argv[])
      {
      float value = 12;
      const CNotSoConstPointer b(&value);
      std::cout << b.GetValue() << std::endl;
    
      b.ModifyWithConst(15);
      std::cout << b.GetValue() << std::endl;
    
      while(!_kbhit()) {}
      return 0;
      }
    

    This will output 12 and then 15, without ever being “clever” about the const-correctness of the const not-so-const object. The reason is that only the pointer ITSELF is const, not the memory it points to.

    If the latter is what you want, you’ll need a lot more wrapping to get the behavior you want, like in my original suggestion below or Iain suggestion.

    ORIGINAL ANSWER:


    You could create a template for your array-proxy, specialized on const-arrays for the const version. The specialized version would have a const *m_pt, return a const pointer, throw an error when you try to set, and so on.

    Edit: Something like this:

    template<typename T>
    class TProxy
      {
      T m_value;
    
      public:
        TProxy(T i_t) : m_value(i_t) {};
    
        template<typename T>
        TProxy(const TProxy<T> &i_rhs) : m_value(i_rhs.m_value) {}
    
        T get() { return m_value; }
        void set(T i_t) { m_value = i_t; }
      };
    
    template<typename T>
    class TProxy<const T *>
      {
      const T *mp_value;
    
      public:
        TProxy(const T *ip_t) : mp_value(ip_t) {};
    
        template<typename T>
        TProxy(const TProxy<T> &i_rhs) : m_value(i_rhs.mp_value) {}
    
        T get() { return m_value; }    
      };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize this is kind of a long question, but I saw no other
First of all, I realize this is a messy situation, but it's not of
I realize this question is very likely to have been asked before, but I've
Sorry I realize this is a very novice question: I am not yet skilled
I realize this may not be the best forum in which to ask this
I realize this is likely a duplicate, but I've been googling/SOing for a day
So I realize this is a possible duplicate question, as there a number of
I'm having some hard time trying to send an email from my app. I
i am having a hard time wrapping my head around how to do this...
Realize this may sound like a broad question - so let me clarify. 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.