Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > const_cast in c...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 43336 of 48044
Post > Topic >>

const_cast in constructors to init const members

by "=?ISO-8859-1?Q?Erik_Wikstr=F6m?=" <Erik-wikstrom@[EMAIL PROTECTED] > Feb 15, 2008 at 01:08 AM

I have a base-class that have some protected members which will be
initialised during construction, however since no derived cl***** (or
members of the base-class) should change the values of those member I
like to make the const. Unfortunately some of them are not trivial to
initialise (i.e. they can not just be initialised from a value passed
to the constructor. There are two ways I can construct these members,
the first is by using helper-functions:

  class Bar {
    // Members
    const size_t m_nr;
    const std::vector<size_t> m_indices;

    // Helper function
    std::vector<size_t> getIndices(const std::vector<size_t>& v,
size_t n) {
      std::vector<size_t> vec;
      for (size_t i =3D 0; i < v.size(); ++i) {
        if (v[i] =3D=3D n) {
          vec.push_back(i);
        }
      }
      return vec;
    }

  public:
    // Constructor
    Bar(size_t n, const std::vector<size_t>& v)
      : m_nr(n), m_indices(getIndices(v, n)) { }
  };

This way works but there are two things that I'm not particularly fond
of: I have to write a function(per member) that will only be used
once, and it can become a bit messy when the initialisation of member
B relies on member A already being initialised.

The alternative is to use const_cast in the constructor to allow it to
make modifications to the const members:

  class Foo {
    // Members
    const size_t m_nr;
    const std::vector<size_t> m_indices;

  public:
    // Constructor
    Foo(size_t n, const std::vector<size_t>& v)
      : m_nr(n)
    {
      for (size_t i =3D 0; i < v.size(); ++i) {
        if (v[i] =3D=3D m_nr) {
          const_cast<std::vector<size_t>& >(m_indices).push_back(i);
        }
      }
    }
  };

The problem with this approach is that it is using const_cast (with
ugly syntax as a result) which I'm not particularly fond of either,
but on the up-side it is easier to read and you do not have to worry
about initialisation order. Is this (modifying the const members)
always safe or is this undefined behaviour? FAQ 18.13* talks a bit
about this but I'm not sure if it applies since it talks about
modifying members in a const member-function.

* http://www.para****ft.com/c++-faq-lite/const-correctness.html#faq-18.13

--
Erik Wikstr=F6m
 




 5 Posts in Topic:
const_cast in constructors to init const members
"=?ISO-8859-1?Q?Erik  2008-02-15 01:08:29 
Re: const_cast in constructors to init const members
"Alf P. Steinbach&qu  2008-02-15 10:44:32 
Re: const_cast in constructors to init const members
"=?ISO-8859-1?Q?Erik  2008-02-15 02:11:44 
Re: const_cast in constructors to init const members
"Alf P. Steinbach&qu  2008-02-15 12:16:42 
Re: const_cast in constructors to init const members
ytrembla@[EMAIL PROTECTED  2008-02-15 16:07:04 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Oct 15 22:32:29 CDT 2008.