Topic: Point of "instantiation" of default destructor?
Author: Nicolas Chapados <chapados@iro.umontreal.ca>
Date: 1999/11/02 Raw View
alan_griffiths@my-deja.com wrote:
> In article <3814701E.6329BBAE@iro.umontreal.ca>,
> Nicolas Chapados <chapados@iro.umontreal.ca> wrote:
> > I recently faced a problem whereby the point of instantiation of
> > the default (compiler-generated) destructor caused difficulties.
> > The problem is related to a combination of templates and inheritance.
> >
> > Consider a basic "intrusive" design for reference-counted objects.
> > SmartPointable is a base class for pointed-to objects, and
> > SmartPointer provides the pointers themselves:
>
> I discuss a similar problem and an applicable solution at:
>
> http://www.octopull.demon.co.uk/arglib/TheGrin.html
Well, my problem was not so much with the design of smart pointer pointer
classes, but rather with when exactly is the default destructor generated
for a class.
In other words:
1- Is the default destructor semantically generated immediately after the
compiler has seen the class definition?
2- Or is it generated at a much later time, when the compiler has seen *a=
ll
the class definitions* which could be of use for generating this default
destructor.
In the example I showed in the original post, the compiler could not
generate the default destructor for class X until it had seen the complet=
e
definition for the class Y passed as argument to a template used as a bas=
e
class of X. Yet, in some circumstances, the default destructor was genera=
ted
just fine, which means that the compiler waited until it had seen the
definition of Y before generating the destructor.
Can I rely on this behaviour?
What does the standard say?
---
Nicolas Chapados http://www.iro.umontreal.ca/~chapados
Universit=E9 de Montr=E9al / Labo. d'informatique des syst=E8mes adaptati=
fs
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Nicolas Chapados <chapados@iro.umontreal.ca>
Date: 1999/10/25 Raw View
I recently faced a problem whereby the point of instantiation of
the default (compiler-generated) destructor caused difficulties.
The problem is related to a combination of templates and inheritance.
Consider a basic "intrusive" design for reference-counted objects.
SmartPointable is a base class for pointed-to objects, and
SmartPointer provides the pointers themselves:
class SmartPointable {
int RefCount;
public:
void incrRef() { RefCount++; }
void decrRef() { RefCount--; }
// ...
};
template<class T>
class SmartPointer {
T* Obj; /* Assume that T inherits from SmartPointable */
public:
~SmartPointer() { Obj->decrRef(); /* plus other stuff */ }
// ...
};
Now, I want to have a specific smart pointer for a certain
forward-declared type (can't use a typedef here for various reasons):
class ComplexClass;
class PComplexClass : public SmartPointer<ComplexClass> {
~PComplexClass(); /* Problem -- see below */
// ...
};
class ComplexClass : public SmartPointable {
// ...
};
The problem is as follows: in PComplexClass, if I don't put any
destructor declaration (so the compiler generates a default dtor),
everything compiles fine (under egcs-2.91.66). Likewise, if I
put the definition of the destructor PComplexClass::~PComplexClass
in the .cc file, everything is also OK.
Yet, if I put the destructor definition inline within the class
definition, I get the error message that decrRef() is undefined
for class ComplexClass. This is to be expected, since at the
point of instantiation of the destructor for
SmartPointer<ComplexClass>, ComplexClass is still only a
forward-declared class.
My question is as follows: given that the code using the default
destructor in PComplexClass compiles correctly, at what point is
this destructor semantially generated by the compiler? Obviously,
it seems to be AFTER the compiler has seen the complete definition
for ComplexClass, but WHEN EXACTLY ???
Thanks!
+ Nicolas
---
Nicolas Chapados http://www.iro.umontreal.ca/~chapados
Universit=E9 de Montr=E9al / Labo. d'informatique des syst=E8mes adaptati=
fs
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: alan_griffiths@my-deja.com
Date: 1999/10/27 Raw View
In article <3814701E.6329BBAE@iro.umontreal.ca>,
Nicolas Chapados <chapados@iro.umontreal.ca> wrote:
> I recently faced a problem whereby the point of instantiation of
> the default (compiler-generated) destructor caused difficulties.
> The problem is related to a combination of templates and inheritance.
>
> Consider a basic "intrusive" design for reference-counted objects.
> SmartPointable is a base class for pointed-to objects, and
> SmartPointer provides the pointers themselves:
I discuss a similar problem and an applicable solution at:
http://www.octopull.demon.co.uk/arglib/TheGrin.html
--
Alan Griffiths (alan.griffiths@experian.com, +44 115 934 4517)
Senior Systems Consultant, Experian
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]