Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #3121
| From | Ruben Safir <ruben@mrbrklyn.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Destructors can copying objects |
| Date | 2011-04-04 22:54 +0000 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <indi6q$r87$7@reader1.panix.com> (permalink) |
| References | (3 earlier) <in9erf$2d82$1@news.ett.com.ua> <dKqdnUe1du6RxwXQnZ2dnUVZ8kGdnZ2d@giganews.com> <indbul$r87$3@reader1.panix.com> <HOSdna4IXtArpQfQnZ2dnUVZ8vOdnZ2d@giganews.com> <indh8e$6mc$1@reader1.panix.com> |
On Mon, 04 Apr 2011 22:38:06 +0000, Ruben Safir wrote:
> On Mon, 04 Apr 2011 22:31:35 +0100, Leigh Johnston wrote:
>
>> On 04/04/2011 22:07, Ruben Safir wrote:
>>> On Sun, 03 Apr 2011 12:29:00 +0100, Leigh Johnston wrote:
>>>
>>>> On 03/04/2011 10:32, Balog Pal wrote:
>>>>> "Ruben Safir"<ruben@mrbrklyn.com>
>>>>>> I don't think I have a copy constructor, which might be the problem
>>>>>> although it never had been.
>>>>>
>>>>> Look up "rule of 3". If you have a dtor, you will need a copy cotor
>>>>> and operator= too, or must disable them.
>>>>>
>>>>>
>>>> Like most "rules" mentioned in this newsgroup the "rule of 3" is a
>>>> nonsense: you may be adding a virtual dtor simply to make a base
>>>> class polymorphic and have no need for a copy ctor and assignment
>>>> operator or you may be relying on RAII and the behaviour of the
>>>> implicitly defined special member functions.
>>>>
>>>> /Leigh
>>>
>>>
>>> Its hard for me to make that judgment though, without a more concrete
>>> understanding of how destructors are triggered with auto values.
>>>
>>> I'm not sure why any allocated memory created with new and dropped on
>>> the heap is auto destructed
>>>
>>>
>> Well the typical example of when to use the rule of 3 is if you have a
>> member variable pointer to allocated memory and it would be incorrect
>> to copy that pointer from one object to another. If this was a pointer
>> to a dynamic array for example then I would use std::vector instead and
>> the rule of 3 requirement disappears as vectors can be safely copied
>> between objects.
>>
>> /Leigh
>
>
>
> If a function creates a new object with new
>
> class myobj{
>
> obj * myfunct(){
> obj* a = new obj;
> return a;
> }
> }
> myobj::~myobj(){
> delete a;
> }
>
> int main(int argv, char ** argc){
>
> obj * out;
> myobj * parent = new myobj;
> out = parent->myfunct();
>
> return 0;
> }
>
>
> Why should this be a problem? The pointer of out should be addressing
> valid memory on the heap.
>
> Ruben
Actually - here is a more coherent example
#include<iostream>
#include<string>
class obj{
public:
obj():a(10), b("Hello World") {}
int a;
std::string b;
};
class myobj{
public:
myobj(){
b = myfunct();
}
~myobj();
obj * b;
obj * myfunct(){
obj* a = new obj;
return a;
}
};
myobj::~myobj(){
delete b;
}
int main(int argv, char ** argc){
obj * out;
obj * out2;
myobj * parent = new myobj;
out = parent->myfunct();
out2 = parent->b;
std::cout << out->a << " " << out->b << std::endl;
return 0;
}
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-02 12:58 +0000
Re: Destructors can copying objects Volker Lukas <vlukas@gmx.de> - 2011-04-02 15:29 +0200
Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-03 02:59 +0000
Re: Destructors can copying objects "Balog Pal" <pasa@lib.hu> - 2011-04-03 11:32 +0200
Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-03 12:29 +0100
Re: Destructors can copying objects Bart van Ingen Schenau <bart@ingen.ddns.info> - 2011-04-04 13:43 +0200
Re: Destructors can copying objects Michael Doubez <michael.doubez@free.fr> - 2011-04-05 01:26 -0700
Re: Destructors can copying objects Joshua Maurice <joshuamaurice@gmail.com> - 2011-04-04 14:03 -0700
Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:10 +0000
Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-04 22:28 +0100
Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:07 +0000
Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-04 22:31 +0100
Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-04 22:43 +0100
Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 22:38 +0000
Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-04 23:42 +0100
Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 22:54 +0000
Re: Destructors can copying objects James Kanze <james.kanze@gmail.com> - 2011-04-03 10:07 -0700
Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:14 +0000
Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:17 +0000
Re: Destructors can copying objects LR <lruss@superlink.net> - 2011-04-05 01:52 -0400
Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-03 03:08 +0000
Re: Destructors can copying objects Paavo Helde <myfirstname@osa.pri.ee> - 2011-04-03 02:11 -0500
Re: Destructors can copying objects Volker Lukas <vlukas@gmx.de> - 2011-04-03 12:02 +0200
Re: Destructors can copying objects Peter Remmers <p.remmers@expires-2011-04-30.arcornews.de> - 2011-04-03 12:14 +0200
Re: Destructors can copying objects Roberto Waltman <usenet@rwaltman.com> - 2011-04-03 18:16 -0400
Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-15 23:06 +0000
Re: Destructors can copying objects LR <lruss@superlink.net> - 2011-04-15 19:19 -0400
Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-15 23:03 +0000
Re: Destructors can copying objects "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-16 01:47 +0200
Re: Destructors can copying objects Peter Remmers <p.remmers@expires-2011-04-30.arcornews.de> - 2011-04-16 13:00 +0200
Re: Destructors can copying objects "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-16 15:57 +0200
Re: Destructors can copying objects LR <lruss@superlink.net> - 2011-04-02 09:51 -0400
Re: Destructors can copying objects Yannick Tremblay <yannick@tremnblay.co.uk> - 2011-04-04 12:56 +0000
Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:05 +0000
Re: Destructors can copying objects Paavo Helde <myfirstname@osa.pri.ee> - 2011-04-04 21:56 -0500
Re: Destructors can copying objects Yannick Tremblay <yannick@tremnblay.co.uk> - 2011-04-05 11:36 +0000
Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-05 15:40 +0000
Re: Destructors can copying objects Sana <sanatakos@gmail.com> - 2011-04-05 10:11 -0700
Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-05 20:28 +0000
Re: Destructors can copying objects Yannick Tremblay <yannick@tremnblay.co.uk> - 2011-04-06 11:12 +0000
Re: Destructors can copying objects drew@furrfu.invalid (Drew Lawson) - 2011-04-05 13:02 +0000
Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-05 15:51 +0000
Re: Destructors can copying objects drew@furrfu.invalid (Drew Lawson) - 2011-04-05 17:47 +0000
Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-05 20:25 +0000
Re: Destructors can copying objects drew@furrfu.invalid (Drew Lawson) - 2011-04-05 20:37 +0000
csiph-web