question about Scott Meyers book

Hi,

In the following code, class Uncopyable makes private any child class copy functions. I understand almost everything here except the destructor. Why should it be declared in Uncopyable? I've tried to comment it and everything works fine without it:

class Uncopyable {
protected: // allow construction
Uncopyable() {} // and destruction of
~Uncopyable() {} // derived objects...
private:
Uncopyable(const Uncopyable&); // ...but prevent copying
Uncopyable& operator=(const Uncopyable&);
};
To keep HomeForSale objects from being copied, all we have to do now
is inherit from Uncopyable:
class HomeForSale: private Uncopyable { // class no longer
... // declares copy ctor or
}; // copy assign. operator
Have you actually tried to create a couple of instances of the class and try to assign one instance to the other?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Uncopyable {
    protected: // allow construction
        Uncopyable() {} // and destruction of
        ~Uncopyable() {} // derived objects...
    private:
        Uncopyable(const Uncopyable&); // ...but prevent copying
        Uncopyable& operator=(const Uncopyable&);
};

class HomeForSale: private Uncopyable {
};

int main()
{
    HomeForSale test1, test2;
    test2 = test1; error: use of deleted function ‘HomeForSale& HomeForSale::operator=(const HomeForSale&)’|
}


Last edited on
Hi jlb,

my question was about the Uncopyable destuctor, not about the operator=
I understand almost everything here except the destructor.


To disallow deletion through a pointer to base class. Uncopyable is not intended to be a polymorphic base class.
Hi cire,

You're right!

Thanks a lot
Topic archived. No new replies allowed.