Const and pointers

Edit1: I understand that it is difficult to understand this question without having an idea of ​​what I'm trying to do. Class A is not complete, but it essentially means a C-array of “proxies” (or “viewer” or “sampler”). In one interesting use, a C-array in the form of a 2d grid is too present (the corresponding function is not shown here). The property of this class is as follows:

  • he should not own the data - without a deep copy
  • it must be copied / assigned
  • it should be light (
  • it should keep a constant (I am having problems with this)

Please do not question the purpose or design: this is the hypothesis of the question.

First 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;
}

A call to "gfi" generates the following output:

1
2

a ac - ac. , m_pt ?

: , /, , .

Edit0: , ( , ).

Edit2: , " " ( , ). , , , , . : http://www.rhinocerus.net/forum/language-c-moderated/569757-const-constructor.html

Edit3: - , . .

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;
}
+3
8

EDIT: , , const-correctness -. :

//--------------------------------------------------------------------------------
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;
  }

12, 15, "" const- const not-so-const. , ITSELF , , .

- , , , , , Iain.

:


-, const- const. const * m_pt, const, ..

: - :

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

:

  • float,
  • , , , .
  • ( ), .

, , .

+2

. , , , .

+1

, . , .

, , , iterator const_iterator, , .

: a const iterator , /, , . const_iterator - , /, , , .

, const float * float *const. A float * const A float *const.

, :

  • .
  • const_A, , iterators do
  • , const A, , A(A & a);
+1

float* float A. float, float*, -, a::get.

0

const ; .

0

, assingment .

Also, returning the handle to the internal data structure is not very good.

0
source

You can reject the copy constructor for certain combinations of arguments:

For example, adding a constructor

A(A& a) :m_pt(a.m_pt) { m_pt = a.m_pt; }

prevents the initialization of any instance of A with const A.

It also prevents const A a2 = a1where a1 is a constant, but you never need to do this, since you can just use a1 directly - it is const, even if you can make a copy, a2 will forever be identical to a1.

0
source

Source: https://habr.com/ru/post/1730184/


All Articles