Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Necessary for copy constructor??

Reply
Thread Tools

Necessary for copy constructor??

 
 
qazmlp1209@rediffmail.com
Guest
Posts: n/a
 
      01-11-2007
Will the following program cause any problem, because of the missing
explicit copy constructor for the 'test' class?

#include <vector>
#include <iostream>

class test
{
char* cPtr ;

public:

test()
{
cPtr = new char[10] ;
}

~test()
{
delete cPtr ;
}
} ;


int main()
{
std::vector< test*> vecTest ;

test* testPtr = new test() ;

vecTest.push_back( testPtr ) ;

delete testPtr ;

vecTest.clear() ;
}

 
Reply With Quote
 
 
 
 
Stuart Redmann
Guest
Posts: n/a
 
      01-11-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Will the following program cause any problem, because of the missing
> explicit copy constructor for the 'test' class?
>
> #include <vector>
> #include <iostream>
>
> class test
> {
> char* cPtr ;
>
> public:
>
> test()
> {
> cPtr = new char[10] ;
> }
>
> ~test()
> {
> delete cPtr ;
> }
> } ;
>
>
> int main()
> {
> std::vector< test*> vecTest ;
>
> test* testPtr = new test() ;
>
> vecTest.push_back( testPtr ) ;
>
> delete testPtr ;
>
> vecTest.clear() ;
> }


Nope. As pointers have their own copy constructor, you can use them in
STL containers freely.

Regards,
Stuart
 
Reply With Quote
 
 
 
 
peter koch
Guest
Posts: n/a
 
      01-11-2007

(E-Mail Removed) skrev:
> Will the following program cause any problem, because of the missing
> explicit copy constructor for the 'test' class?


Yes. You will have double deletes. Depending on your real needs, you
should use std::vector or std::string instead of the lowlevel pointer.
In fact, operator new[] should almost never be used. Prefer std::vector
(or in rare situations raw operator new)

/Peter
>
> #include <vector>
> #include <iostream>
>
> class test
> {
> char* cPtr ;
>
> public:
>
> test()
> {
> cPtr = new char[10] ;
> }
>
> ~test()
> {
> delete cPtr ;
> }
> } ;
>
>
> int main()
> {
> std::vector< test*> vecTest ;
>
> test* testPtr = new test() ;
>
> vecTest.push_back( testPtr ) ;
>
> delete testPtr ;
>
> vecTest.clear() ;
> }


 
Reply With Quote
 
qazmlp1209@rediffmail.com
Guest
Posts: n/a
 
      01-11-2007
peter koch wrote:
> (E-Mail Removed) skrev:
> > Will the following program cause any problem, because of the missing
> > explicit copy constructor for the 'test' class?

>
> Yes. You will have double deletes.

You are right, in general (Every class having a pointer member should
have an explicit constructor defined correctly).
But, could you comment whether it will cause problem for the code that
I have given within main()?

> Depending on your real needs, you
> should use std::vector or std::string instead of the lowlevel pointer.
> In fact, operator new[] should almost never be used. Prefer std::vector
> (or in rare situations raw operator new)


 
Reply With Quote
 
dasjotre
Guest
Posts: n/a
 
      01-11-2007

(E-Mail Removed) wrote:
> Will the following program cause any problem, because of the missing
> explicit copy constructor for the 'test' class?


I don't think that the copy constructor is generated in this case
since it is not used.

> But, could you comment whether it will cause problem for the code that
> I have given within main()?


since only the pointer is copied and not the object it poins to the
answer is no.

( delete cPtr should be delete [] cPtr )

 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      01-11-2007

(E-Mail Removed) skrev:
> peter koch wrote:
> > (E-Mail Removed) skrev:
> > > Will the following program cause any problem, because of the missing
> > > explicit copy constructor for the 'test' class?

> >
> > Yes. You will have double deletes.

> You are right, in general (Every class having a pointer member should
> have an explicit constructor defined correctly).
> But, could you comment whether it will cause problem for the code that
> I have given within main()?


It is not. But this is no excuse for disabling copy-construction (and
assignment).

/Peter

 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      01-11-2007
Stuart Redmann wrote:
> (E-Mail Removed) wrote:
>> Will the following program cause any problem, because of the missing
>> explicit copy constructor for the 'test' class?
>>
>> #include <vector>
>> #include <iostream>
>>
>> class test
>> {
>> char* cPtr ;
>>
>> public:
>>
>> test()
>> {
>> cPtr = new char[10] ;
>> }
>>
>> ~test()
>> {
>> delete cPtr ;
>> }
>> } ;
>>
>>
>> int main()
>> {
>> std::vector< test*> vecTest ;
>>
>> test* testPtr = new test() ;
>>
>> vecTest.push_back( testPtr ) ;
>>
>> delete testPtr ;
>>
>> vecTest.clear() ;
>> }

>
> Nope. As pointers have their own copy constructor, you can use them in
> STL containers freely.


Woah there! That's horrible advice. Without a copy constructor, using
such an object in an STL container will invariably lead to
double-deletes (i.e. undefined behavior).

Remember the rule of three: Generally, if a class requires any of the
following three to be implemented then it requires that *all* of the
three be implemented:

destructor
assignment operator
copy constructor

--
Clark S. Cox III
(E-Mail Removed)
 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      01-11-2007
dasjotre wrote:
> (E-Mail Removed) wrote:
>> Will the following program cause any problem, because of the missing
>> explicit copy constructor for the 'test' class?

>
> I don't think that the copy constructor is generated in this case
> since it is not used.


Conceptually, the copy constructor is always generated unless you
provide one. Whether that has an impact on the final output of the
compiler when it isn't called is platform dependent.

> > But, could you comment whether it will cause problem for the code that
> > I have given within main()?

>
> since only the pointer is copied and not the object it poins to the
> answer is no.
>
> ( delete cPtr should be delete [] cPtr )
>



--
Clark S. Cox III
(E-Mail Removed)
 
Reply With Quote
 
Eberhard Schefold
Guest
Posts: n/a
 
      01-11-2007
Clark S. Cox III wrote:

> Woah there! That's horrible advice. Without a copy constructor, using
> such an object in an STL container will invariably lead to
> double-deletes (i.e. undefined behavior).


The point is here that not the object is used in the container. A
pointer to the object is. The example as shown has no problems (apart
from the "delete" instead of "delete[]"). Even if the object is not
copy-able, you can still use pointers in a container.

What I would do is to make the class explicitly non-copyable and
non-assignable, so the compiler can report any misuse.
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      01-11-2007
Clark S. Cox III wrote:
> Stuart Redmann wrote:
>> As pointers have their own copy constructor, you can use them in
>> STL containers freely.

>
> Woah there! That's horrible advice. Without a copy constructor, using
> such an object in an STL container will invariably lead to
> double-deletes (i.e. undefined behavior).
>


Whoa, again! In the specific context of the original example, there is
no problem with double deletes. Pointers do, in fact, have valid copy
semantics. There is, as you suggest, a possible issue of keeping track
of ownership, but there's nothing wrong with the original code. It
correctly deletes the one object that it creates.

If you want a container to also manage lifetimes, then you've got a
significant design problem: you also have to make sure that every other
piece of code is clear on ownership. But there are situations where it's
appropriate for a container to manage pointers but not worry about
lifetimes, such as maintaining a list of currently active objects from a
set of statically allocated ones.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Q: why are all templated copy-constructors necessary? Claudius C++ 8 07-23-2007 05:48 AM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
is a digital filter necessary? nesppi@gmail.com VHDL 1 01-13-2006 11:37 AM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
cookie handling - is there a list of *necessary* cookies ?? cookie-monster Firefox 1 03-05-2005 03:40 AM



Advertisments