![]() |
|
|
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. |
|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
![]() |
|
| Thread Tools | Search this Thread |
| Display Modes | |
|
|