-3

I met a interesting error when I tried to use copy-and-swap idiom in an abstract class. The following code is specially created to demonstrate this situation, if there is anything improper, tell me but don't focus:

template <typename T> class iter
{
public:
    iter();
    virtual ~iter();
    iter(const iter &);
    iter(iter &&) noexcept;
    iter& operator=(iter);
    virtual void swap(iter &);

    virtual bool operator!=(const iter&);
    virtual bool operator==(const iter&);
    virtual bool operator++() = 0;
    virtual bool operator--() = 0;

private:
    T* pointer;
};

And the error message is:

iterator.h:10:18: error: cannot declare parameter to be of abstract       type 'iter<T>'
iter& operator=(iter);
              ^
iterator.h:3:28: note:   because the following virtual functions are pure within 'tier':
iterator.h:15:15: note:     virtual bool iter<T>::operator++()
virtual bool operator++() = 0;
           ^
iterator.h:16:15: note:     virtual bool iter<T>::operator--()
virtual bool operator--() = 0;

Clearly it is this line causes this error, so do I have to declare it in derived class instead of here, or there is any other advanced technique?

iter& operator=(iter);
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
  • Guys: please be noted this is copy-and-swap idiom, so the overload of operaoter=(iter) use copy rather const reference is on purpose. Please read some thing more about copy-and-swap, otherwise don't down vote. – Xiangyu Zhang May 22 '16 at 21:43
  • A disclaimer won't bend the rules of the language, you cannot do that and there are already two nice answers that explain why. – skypjack May 22 '16 at 21:48
  • You cannot use copy-and-swap for abstract class (as this example shows) – M.M May 22 '16 at 21:55
  • Create an implementations for your 2 pure functions, even if all they do is throw. Also given that this class is meant to be derived from your `operator=` will slice as others have told you it is taking the wrong parameter type. – Richard Critten May 22 '16 at 22:03

2 Answers2

3
iterator.h:10:18: error: cannot declare parameter to be of abstract
                         type 'iter<T>'
iter& operator=(iter);

The assignment operator should take its parameter as a const reference:

iter &operator=(const iter &);

The reason for the compilation error should be very obvious: passing a parameter by value necessitates that the parameter must be copied, which can't be done with an abstract class, by default.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Guys: please be noted this is copy-and-swap idiom, so the overload of operaoter=(iter) use copy rather const reference is on purpose. Please read some thing more about copy-and-swap, otherwise don't down vote. – Xiangyu Zhang May 22 '16 at 21:45
3

Your assignment operator must take a reference:

iter& operator=(iter const&);

There won't/can't ever be an iter instance alone in the program, there will be only derived class instances that implemented the abstract functions. You can't slice that off and make a copy of an abstract base class only.

Do I have to declare it in derived class instead of here?

Sounds like the way to go.

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
  • Guys: please be noted this is copy-and-swap idiom, so the overload of operaoter=(iter) use copy rather const reference is on purpose. Please read some thing more about copy-and-swap, otherwise don't down vote. – Xiangyu Zhang May 22 '16 at 21:45