Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Foo {
- Foo* next = nullptr;
- public:
- Foo* getNext() { return next; }
- const Foo* getNextConst() const { return next; }
- const Foo &operator= (const Foo &foo) const {
- *this = foo;
- return *this;
- }
- Foo(Foo&) = default;
- Foo& operator=(Foo&) = default;
- Foo() {
- }
- private:
- Foo(const Foo&) = default;
- Foo& operator=(const Foo&) = default;
- };
- int main() {
- // This works as expected
- Foo foo;
- foo.getNext();
- foo.getNextConst();
- // This works as expected too
- const Foo fooConst;
- //fooConst.getNext(); // error!
- fooConst.getNextConst();
- //foo = fooConst; // error!
- // This isn't const-correct, as we can now access the non-const version of Foo.
- foo.getNext();
- foo.getNextConst();
- // This should be possible
- fooConst = foo;
- const Foo fooConst2(foo);
- fooConst = fooConst;
- //const Foo fooConst3 = fooConst; // not supported
- foo = foo;
- Foo foo2(foo);
- }
Add Comment
Please, Sign In to add comment