Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to understand that there are both "virtual ~Number();" and "Number::~Number()"?

19 views
Skip to first unread message

fl

unread,
Jun 14, 2015, 11:31:21 PM6/14/15
to
Hi,

When I read the example class, I feel puzzled about "Number::~Number()"
after there is

virtual ~Number();

I remember that the derived class of Number should have a real
~Number();


Now, class Number has both virtual and member function
~Number().


Can you help me on this question?


Thanks for the expert keeping answer my questions.






////////////////////////////
struct BaseConstructor
{
BaseConstructor(int=0)
{}
};

class RealNumber;
class Complex;
class Number;
class Number
{
friend class RealNumber;
friend class Complex;
public:
Number ();
Number & operator = (const Number &n);
Number (const Number &n);
virtual ~Number();
virtual Number operator + (Number const &n) const;
void swap (Number &n) throw ();

static Number makeReal (double r);
static Number makeComplex (double rpart, double ipart);
protected:
Number (BaseConstructor);

private:
void redefine (Number *n);
virtual Number complexAdd (Number const &n) const;
virtual Number realAdd (Number const &n) const;

Number *rep;
short referenceCount;
};

Number::~Number()
{
if (rep && --rep->referenceCount == 0)
delete rep;
}

Richard

unread,
Jun 14, 2015, 11:53:07 PM6/14/15
to
[Please do not mail me a copy of your followup]

What book is this you're reading so I can make sure *not* to recommend
it to anyone? LOL. I find the snippets you're posting to leave a
reader new to the language more confused than enlightened and they
often seem to exhibit poor practices and conventions. Introducing
someone to the language with poor practices and conventions is IMO a
really bad way to teach C++.

fl <rxj...@gmail.com> spake the secret code
<265510a8-466d-4f31...@googlegroups.com> thusly:

>I remember that the derived class of Number should have a real
> ~Number();

If a class is designed to be used as a base class with virtual methods
that provide 'extension points' from the base class, such a base class
should have a virtual destructor.

The reason for this is that virtual methods are the mechanism by which
we obtain run-time polymorphism for a class hierarchy. In such a
sitaution, the user of such class is interacting with the class
through a pointer to the base class. When an instance of the class is
destroyed by operator delete, the virtual destructor ensures that the
destructor associated with the actual derived instance of the base is
invoked. If the destructor were not virtual, only the base class
would be destroyed and the object would not be properly destroyed.

>Now, class Number has both virtual and member function
>~Number().

It declares the destructor virtual and then defines the destructor.

> virtual ~Number();

This is a declaration.

>Number::~Number()
>{
> if (rep && --rep->referenceCount == 0)
> delete rep;
>}

This is a definition. When a virtual method is defined outside the
class declaration, the 'virtual' keyword is not allowed because only
declarations are marked virtual, not definitions.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Paavo Helde

unread,
Jun 15, 2015, 2:23:47 AM6/15/15
to
fl <rxj...@gmail.com> wrote in news:265510a8-466d-4f31-b63a-fb8c1e210cc0
@googlegroups.com:

> Hi,
>
> When I read the example class, I feel puzzled about "Number::~Number()"
> after there is
>
> virtual ~Number();
>
> I remember that the derived class of Number should have a real
> ~Number();

It looks like you are thinking that a virtual function may not have a
definition. This is not what 'virtual' means in C++, even pure virtual
functions can have definitions. Find a decent C++ tutorial and reread the
introduction to virtual member functions.
0 new messages