0

I'm trying to modify an array of objects which have const members:

enum Bar {
    Baz,
    Qux,
    Quux
};

class Foo {
    public:
    Foo(Bar a, int b): a_(a), b_(b) {};

    private:
    const Bar a_;
    const int b_;
};

int main(int argc, char* argv[]) {
    Bar b[] = {
        Baz,
        Baz
    };

    // This works fine
    b[0] = Qux;

    Foo f[] = {
        Foo(Baz,42),
        Foo(Qux,32)
    };

    // This doesn't
    f[0] = Foo(Quux,3);

    return 0;
}

But the compiler wouldn't let me:

$ make test
g++     test.cc   -o test
test.cc: In member function ‘Foo& Foo::operator=(const Foo&)’:
test.cc:7:7: error: non-static const member ‘const Bar Foo::a_’, can’t use default assignment operator
test.cc:7:7: error: non-static const member ‘const int Foo::b_’, can’t use default assignment operator
test.cc: In function ‘int main(int, char**)’:
test.cc:31:22: note: synthesised method ‘Foo& Foo::operator=(const Foo&)’ first required here 
make: *** [test] Error 1

I'm sure the compiler has its reasons and I'm eager to learn why the code is not meant to work.

And I do also want to know how to make the intended changes to the f array.

Right now, the following does the job for me, but it looks so wrong:

#include <cstring>
#include <iostream>

enum Bar {
    Baz,
    Qux,
    Quux
};

class Foo {
    public:
    Foo(Bar a, int b): a_(a), b_(b) {};

    /*Foo &operator=(Foo const& f) {
     return f;
    }*/

    const Bar a_;
    const int b_;
};

int main(int argc, char* argv[]) {
    Bar b[] = {
        Baz,
        Baz
    };

    // This works fine
    b[0] = Qux;

    Foo f[] = {
        Foo(Baz,42),
        Foo(Qux,32)
    };

    // This doesn't
    //f[0] = Foo(Quux,3);

    // This does...
    Foo foo1(Quux, 344);
    memcpy(&f[0], &foo1, sizeof(foo1));

    std::cout << "Hi " << f[0].b_ <<"\n";
    return 0;
}

I'd appreciate a solution that doesn't involve memcpy but still changes the array in the desired way.

3
  • 3
    Arrays have nothing to do with it. Foo a, b; a = b; should also fail to compile, as the compiler does not know how to assign to a value which is const. Commented Oct 4, 2013 at 16:58
  • @K-ballo: That's an answer, not a comment. Please make an answer that can be accepted and upvoted and reread the FAQ ;) Commented Oct 4, 2013 at 17:00
  • Hm, I don't want to assign a value to the Foo, but rather update the 0th element in the f array. Just like it's done in the array of Bars. Commented Oct 4, 2013 at 17:03

2 Answers 2

3

Arrays have nothing to do with it.

Foo a, b;
a = b;

should also fail to compile, as the compiler does not know how to assign to a value which is const.

Sign up to request clarification or add additional context in comments.

3 Comments

Hm, I don't want to assign a value to the Foo, but rather update the 0th element in the f array. Just like it's done in the array of Bars.
But you can't update the element in the array without assigning it.
@FrederickNord: There is no "update the element in the array". An element in the array is just as any other variable, if you do an assignment then you are assigning to it.
1

You can't change the value of const members. But f[0] = Foo(Quux,3); would do nothing than f[0].a_ = Quux; f[0].b_ = 3; and since both are const members, it fails to compile.

The only think you can do here is using pointers, eg:

#include <memory>

int main(int argc, char* argv[]) {
    // ...
    std::unique_ptr<Foo> f[] = {
        std::unique_ptr<Foo>(new Foo(Baz, 42)),
        std::unique_ptr<Foo>(new Foo(Qux, 32))
    };

    f[0].reset(new Foo(Quux, 4));
}

If you using gcc, you have to use the -std=cpp11 flag. unique_ptr is defined in the header "memory".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.