Guest User

Untitled

a guest
Jun 13th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. class Foo {
  2.   Foo* next = nullptr;
  3. public:
  4.   Foo* getNext() { return next; }
  5.   const Foo* getNextConst() const { return next; }
  6.  
  7.   const Foo &operator= (const Foo &foo) const {
  8.       *this = foo;
  9.       return *this;
  10.   }
  11.  
  12.   Foo(Foo&) = default;
  13.   Foo& operator=(Foo&) = default;
  14.  
  15.   Foo() {
  16.   }
  17.  
  18. private:
  19.   Foo(const Foo&) = default;
  20.   Foo& operator=(const Foo&) = default;
  21. };
  22.  
  23. int main() {
  24.   // This works as expected
  25.   Foo foo;
  26.   foo.getNext();
  27.   foo.getNextConst();
  28.   // This works as expected too
  29.   const Foo fooConst;
  30.   //fooConst.getNext(); // error!
  31.   fooConst.getNextConst();
  32.   //foo = fooConst; // error!
  33.   // This isn't const-correct, as we can now access the non-const version of Foo.
  34.   foo.getNext();
  35.   foo.getNextConst();
  36.   // This should be possible
  37.   fooConst = foo;
  38.   const Foo fooConst2(foo);
  39.  
  40.   fooConst = fooConst;
  41.   //const Foo fooConst3 = fooConst; // not supported
  42.  
  43.   foo = foo;
  44.   Foo foo2(foo);
  45. }
Add Comment
Please, Sign In to add comment