Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-14-2008, 07:03 AM
rkldabs@gmail.com
 
Posts: n/a
Destructor

Hi,

This one might be really very basic, but am unable to correlate the
understanding with the C++ standard (N2691).

Consider the class

class A{
private:
string ms;
};

Here are the two quotes that I am focussing on from the standard:

$12.4/3- "If a class has no user-declared destructor, a destructor is
declared implicitly..."
$12.4/5- "Before the non-user-provided destructor for a class is
implicitly defined, all the non-user-defined destructors for its base
classes and its non-static data members shall have been implicitly
defined."

In this case, class string provides it's own user defined destructor.
This means that as per $12.4/5, class A cannot have an implicit
destructor. If class A does not have an implicit destructor and
neither a user provided destructor, then how are objects of class A
cleaned up?

I think this is a pretty silly point and I am sure I am missing
something important here. What is that?

Regards,
Dabs.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #2 (permalink)  
Old 08-14-2008, 11:31 PM
schildmeijer@gmail.com
 
Posts: n/a
Re: Destructor

> Hi,
>
> This one might be really very basic, but am unable to correlate the
> understanding with the C++ standard (N2691).
>
> Consider the class
>
> class A{
> private:
> string ms;
>
> };
>
> Here are the two quotes that I am focussing on from the standard:
>
> $12.4/3- "If a class has no user-declared destructor, a destructor is
> declared implicitly..."


Your class A (which has no user-declared destructor) will get a
syntezied destructor that looks something like this:
~A() {}

> $12.4/5- "Before the non-user-provided destructor for a class is
> implicitly defined, all the non-user-defined destructors for its base
> classes and its non-static data members shall have been implicitly
> defined."
>
> In this case, class string provides it's own user defined destructor.
> This means that as per $12.4/5, class A cannot have an implicit
> destructor. If class A does not have an implicit destructor and
> neither a user provided destructor, then how are objects of class A
> cleaned up?


You need to read between the lines. Read the sentece like this:
"the non-user-defined destructors for its base
> classes and its non-static data members shall have been implicitly (OR EXPLICITLY)
> defined."


>
> I think this is a pretty silly point and I am sure I am missing
> something important here. What is that?



HTH Roger Schildmeijer

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #3 (permalink)  
Old 08-14-2008, 11:33 PM
JoshuaMaurice@gmail.com
 
Posts: n/a
Re: Destructor

On Aug 13, 11:03 pm, rkld...@gmail.com wrote:
> class A{
> private:
> string ms;
>
> };

....
> In this case, class string provides it's own user defined destructor.
> This means that as per $12.4/5, class A cannot have an implicit
> destructor. If class A does not have an implicit destructor and
> neither a user provided destructor, then how are objects of class A
> cleaned up?


A member of class A (string ms;) has an explicitly defined destructor.
This does not mean class A itself has an explicitly defined
destructor. class A lacks an explicitly defined destructor. Thus a
compiler will implicitly define a destructor for class A. All is
well.

Now, you cannot create a class without a destructor. Either you
declare one, or the compiler defines one. If the destructor is not
accessible in the scope that the object is being destroyed, you'll get
a compile-time error, thus it's impossible to have this "But how are
objects of class A cleaned up?" problem of yours. (Note that I'm not
saying memory leaks are impossible. I'm just saying that when an
object's lifetime ends, its destructor is guaranteed to exist, be
accessible, and be called, for a well formed program.)

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #4 (permalink)  
Old 08-14-2008, 11:36 PM
Maciej Sobczak
 
Posts: n/a
Re: Destructor

On 14 Sie, 08:03, rkld...@gmail.com wrote:

> Here are the two quotes that I am focussing on from the standard:
>
> $12.4/3- "If a class has no user-declared destructor, a destructor is
> declared implicitly..."
> $12.4/5- "Before the non-user-provided destructor for a class is
> implicitly defined, all the non-user-defined destructors for its base
> classes and its non-static data members shall have been implicitly
> defined."


Which relates to all those destructors for base classes and non-static
data members that are non-user-defined. It does not relate to other
destructors (user-defined).

> In this case, class string provides it's own user defined destructor.


And therefore is not covered by the second quote above.

> This means that as per $12.4/5

[...]

No, this point does not apply here, exactly because the string class
has user-defined destructor.

> how are objects of class A
> cleaned up?


12.4/6:

"A destructor for class X calls the destructors for X’s direct
members, ..."

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #5 (permalink)  
Old 08-14-2008, 11:38 PM
Nicola Musatti
 
Posts: n/a
Re: Destructor

On Aug 14, 8:03 am, rkld...@gmail.com wrote:
> Hi,
>
> This one might be really very basic, but am unable to correlate the
> understanding with the C++ standard (N2691).
>
> Consider the class
>
> class A{
> private:
> string ms;
>
> };
>
> Here are the two quotes that I am focussing on from the standard:
>
> $12.4/3- "If a class has no user-declared destructor, a destructor is
> declared implicitly..."
> $12.4/5- "Before the non-user-provided destructor for a class is
> implicitly defined, all the non-user-defined destructors for its base
> classes and its non-static data members shall have been implicitly
> defined."


Note that it says: *non-user-defined* destructors.

> In this case, class string provides it's own user defined destructor.
> This means that as per $12.4/5, class A cannot have an implicit
> destructor. If class A does not have an implicit destructor and
> neither a user provided destructor, then how are objects of class A
> cleaned up?


As you just wrote std::string's destructor *is* user-defined, so
12.4/5 above doesn't apply and your class A can and indeed does have
an implicit destructor.

Cheers,
Nicola Musatti



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #6 (permalink)  
Old 08-14-2008, 11:41 PM
Richard Corden
 
Posts: n/a
Re: Destructor

Hi,

rkldabs@gmail.com wrote:
> Hi,
>
> This one might be really very basic, but am unable to correlate the
> understanding with the C++ standard (N2691).
>
> Consider the class
>
> class A{
> private:
> string ms;
> };
>
> Here are the two quotes that I am focussing on from the standard:
>
> $12.4/3- "If a class has no user-declared destructor, a destructor is
> declared implicitly..."
> $12.4/5- "Before the non-user-provided destructor for a class is
> implicitly defined, all the non-user-defined destructors for its base
> classes and its non-static data members shall have been implicitly
> defined."
>
> In this case, class string provides it's own user defined destructor.
> This means that as per $12.4/5, class A cannot have an implicit
> destructor. If class A does not have an implicit destructor and
> neither a user provided destructor, then how are objects of class A
> cleaned up?



12.4/5 is specifying the order that the compiler shall define the
destructors. It is saying something like: Before the destructor for
this class is implicitly defined, if any of the base classes or non
static data members also require the destructors to be implicitly
defined, then they shall be defined first.

For example:

class C
{
public:
C();
};

class D
{
public:
D ();

private:
C c;
};

12.4/5 means that the compiler shall define '~C()' before it defines '~D()'.



12.4/5 doesn't apply to "class A" in your original example, as none of
its base classes or non static data members have implicitly declared
destructors.


I hope this helps!


Richard



--
Richard Corden

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #7 (permalink)  
Old 08-15-2008, 01:17 AM
Francis Glassborow
 
Posts: n/a
Re: Destructor

rkldabs@gmail.com wrote:
> Hi,
>
> This one might be really very basic, but am unable to correlate the
> understanding with the C++ standard (N2691).
>
> Consider the class
>
> class A{
> private:
> string ms;
> };
>
> Here are the two quotes that I am focussing on from the standard:
>
> $12.4/3- "If a class has no user-declared destructor, a destructor is
> declared implicitly..."
> $12.4/5- "Before the non-user-provided destructor for a class is
> implicitly defined, all the non-user-defined destructors for its base
> classes and its non-static data members shall have been implicitly
> defined."
>
> In this case, class string provides it's own user defined destructor.
> This means that as per $12.4/5, class A cannot have an implicit
> destructor. If class A does not have an implicit destructor and
> neither a user provided destructor, then how are objects of class A
> cleaned up?
>
> I think this is a pretty silly point and I am sure I am missing
> something important here. What is that?
>

I think you are having problems with reading Standardese. 12.4/5 is
there to provide an ordering for implicitly defined dtors. Consider:

struct base {
int i;
};

Note that just writing that does not cause ~base() to be defined, an
object or sub-object of type base must be declared for that to happen.

struct member {
double d;
};

As for struct base.

struct derived: base {
member m;
};

Still no implicitly defined dtors.


int main(){
derived d;
}

Now a dtor must be defined for derived and the rules say that that
triggers the definition of ~base() and ~member(). Though it does not say
so explicitly, the order will be the normal left to right depth first,
members in order of declaration.

In a case like the one above where all the dtors are trivial it does not
really matter but in cases where there are a mixture of user defined
dtors and implicitly defined ones, having a defined order avoids any
possibility of undefined behaviour caused by a bad ordering.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #8 (permalink)  
Old 08-15-2008, 01:21 AM
Oncaphillis
 
Posts: n/a
Re: Destructor

rkldabs@gmail.com wrote:

> $12.4/5- "Before the non-user-provided destructor for a class is
> implicitly defined, all the non-user-defined destructors for its base
> classes and its non-static data members shall have been implicitly
> defined."


I don't interpret this one to forbid A to have an implicitly defined
DTor if one of it's base classes/members have an explicitly defined
DTor. It just says if there is an implicitly defined DTor for such
a class it shall be defined before the one for A.

O.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #9 (permalink)  
Old 08-15-2008, 01:29 AM
Salt_Peter
 
Posts: n/a
Re: Destructor

On Aug 14, 2:03 am, rkld...@gmail.com wrote:
> Hi,
>
> This one might be really very basic, but am unable to correlate the
> understanding with the C++ standard (N2691).
>
> Consider the class
>
> class A{
> private:
> string ms;
>
> };
>
> Here are the two quotes that I am focussing on from the standard:
>
> $12.4/3- "If a class has no user-declared destructor, a destructor is
> declared implicitly..."
> $12.4/5- "Before the non-user-provided destructor for a class is
> implicitly defined, all the non-user-defined destructors for its base
> classes and its non-static data members shall have been implicitly
> defined."
>
> In this case, class string provides it's own user defined destructor.
> This means that as per $12.4/5, class A cannot have an implicit
> destructor. If class A does not have an implicit destructor and
> neither a user provided destructor, then how are objects of class A
> cleaned up?


$12.4/5 doesn't say that user defined destructors are ignored, at
least thats my interpretation. The statement says before an implicit
d~tor is defined, any d~tor that is not user-defined for both base and
members must be implictly defined first. In essence, we are building
d~tors from the bottom up.

This makes sense since the d~tor at the top of the chain (the derived
entity and/or containing entity) invokes the member and base d~tors to
completion.

>
> I think this is a pretty silly point and I am sure I am missing
> something important here. What is that?
>
> Regards,
> Dabs.
>
> --
> [ Seehttp://www.gotw.ca/resources/clcm.htmfor info about ]
> [ comp.lang.c++.moderated. First time posters: Do this! ]



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #10 (permalink)  
Old 08-15-2008, 01:29 AM
Markus Moll
 
Posts: n/a
Re: Destructor

Hi

rkldabs@gmail.com wrote:

> class A{
> private:
> string ms;
> };

[...]
> $12.4/3- "If a class has no user-declared destructor, a destructor is
> declared implicitly..."
> $12.4/5- "Before the non-user-provided destructor for a class is
> implicitly defined, all the non-user-defined destructors for its base
> classes and its non-static data members shall have been implicitly
> defined."
>
> In this case, class string provides it's own user defined destructor.
> This means that as per $12.4/5, class A cannot have an implicit
> destructor. If class A does not have an implicit destructor and
> neither a user provided destructor, then how are objects of class A
> cleaned up?
>
> I think this is a pretty silly point and I am sure I am missing
> something important here. What is that?


Quantification.
If there are no non-user-provided destructors, then all of them are
implicitly defined (exercise 1.1: try to find one which isn't).
So your conclusion "as per $12.4/5, class A cannot have an implicit
destructor" is false. Instead, class A does indeed have an implicit
destructor.

Markus


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 01:50 PM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Mobile Phone | Car Loans | Credit Cards | Credit Check | Secured Loans



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114