开发者

Passing a const vector<pointers> to a method but changing value pointed to

I have following code (only relevant portions shown for sake of brevity - please let me know if I have been too brief):

class my_class
{
    public:
    my_class() {m_i=0;}
    set(int i) {m_i = i;}
    private:
    int m_i;
}

void CallMod()
{
    // create a bunch of my_class* o = new my_class() and store in vector<my_class*> 
    // vObject (left out for brevity)
    Mod(vObject);
    // will vObject contain pointers to objects that have m_i == 2
}

void Mod(vector<my_class*> const & vObject)
{
    BOOST_FOREACH(my_class o, vObject)
    {
        o->set(2);
    }
}

Does this mean that while vObject is const, the modification done by the call to o->set(2) will be retained once Mod returns? Does that indicate that the "const" qualifier will not allow modify operations on vObject (i.e. the vector) but allows modification on the contained pointers to my_class?

Did I understand this right? Any duplicate questions that answers this - I couldn't find one - links mos开发者_JAVA百科t appreciated.


The vector will be const. You can only get const_iterators from it. You can't modify it or it's elements.

The elements in the container will be const pointers. Unfortunately, a const pointer doesn't mean the element it points to is const, just that the value of the pointer can't change.

If you had a vector<my_class> instead of vector<my_class*>, you would not be able to modify the my_class objects inside the const vector (except if you casted away the const-ness, obviously).


Your assessment is correct. You cannot modify vObject's members or call non-const member functions on it, but you can modify members and call non-const members functions of my_class for any of the pointers in the vector.


It means that you can not modify (insert/delete) the vector vObject in Mod() function. It also means that you can not modify the pointers to my_class inside Mod() function. But you can modify whatever these pointers are pointing to. The rule to modify const pointer and element the const pointer points to applies here as well.


First, think about const and pointers to understand why this is the behavior. Here are the different possibilities for a basic pointer. (Assume you have two dynamically allocated char arrays: char* hello and char* goodbye initialized to their equivalently named strings.)

Nothing is const:

char* ptr = hello;
ptr[0] = 'j';  // Legal
ptr = goodbye; // Legal

Pointer to const:

const char* ptr = hello;
ptr[0] = 'j';  // ILLEGAL - individual characters are const
ptr = goodbye; // Legal

Const pointer:

char* const ptr = hello;
ptr[0] = 'j';  // Legal
ptr = goodbye; // ILLEGAL - ptr itself is const

Const pointer to const:

const char* const ptr = hello;
ptr[0] = 'j';  // ILLEGAL - individual characters are const
ptr = goodbye; // ILLEGAL - ptr itself is const

Dereferencing a const_iterator returns a const reference to its stored object. Since the object the iterator points to is a my_class*, then the result is a const pointer (i.e., my_class* const). As shown above, you cannot re-assign a const pointer, but you can call non-const methods on the object it points to. If it was a vector<const my_class*>, then you wouldn't be able to modify the underlying objects, even with a non-const iterator.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜