Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Creating a pair of objects with no copy constructor?

Reply
Thread Tools

Creating a pair of objects with no copy constructor?

 
 
responsible
Guest
Posts: n/a
 
      07-20-2007
Hi,
Assume I have the following class

class test{
public:
int x;
test(){
x = 5;
}
private:
test(test& v){
x = v.x;
}
};

now, i need to create a pair of this object
pair<test,test>(??,??)
what should I put in the constructor to allow me to construct such a
pair? All attempts fail because copy constructor is disallowed.

Thank you very much

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-20-2007
responsible wrote:
> Assume I have the following class
>
> class test{
> public:
> int x;
> test(){
> x = 5;
> }
> private:
> test(test& v){
> x = v.x;
> }
> };
>
> now, i need to create a pair of this object
> pair<test,test>(??,??)


You can't. std:air requires the object to be copy-constructible.

> what should I put in the constructor to allow me to construct such a
> pair? All attempts fail because copy constructor is disallowed.


You're SOL

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      07-20-2007
On 2007-07-20 19:51, Victor Bazarov wrote:
> responsible wrote:
>> Assume I have the following class
>>
>> class test{
>> public:
>> int x;
>> test(){
>> x = 5;
>> }
>> private:
>> test(test& v){
>> x = v.x;
>> }
>> };
>>
>> now, i need to create a pair of this object
>> pair<test,test>(??,??)

>
> You can't. std:air requires the object to be copy-constructible.


And you'll find that a lot of other stuff from the standard library also
requires the objects or be copyable, another common requirement is that
the objects should be assignable.

--
Erik Wikström
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-20-2007
Erik Wikström wrote:
> On 2007-07-20 19:51, Victor Bazarov wrote:
>> responsible wrote:
>>> Assume I have the following class
>>>
>>> class test{
>>> public:
>>> int x;
>>> test(){
>>> x = 5;
>>> }
>>> private:
>>> test(test& v){
>>> x = v.x;
>>> }
>>> };
>>>
>>> now, i need to create a pair of this object
>>> pair<test,test>(??,??)

>>
>> You can't. std:air requires the object to be copy-constructible.

>
> And you'll find that a lot of other stuff from the standard library
> also requires the objects or be copyable, another common requirement
> is that the objects should be assignable.


Right. And less-than comparable, and equal comparable, for some
algorithms... Clause 20 begins with the list of requirements for
the objects for which standard algorithms and structures are going
to be instantiated. Interesting reading...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      07-21-2007
"responsible" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hi,
> Assume I have the following class
>
> class test{
> public:
> int x;
> test(){
> x = 5;
> }
> private:
> test(test& v){
> x = v.x;
> }
> };
>
> now, i need to create a pair of this object
> pair<test,test>(??,??)
> what should I put in the constructor to allow me to construct such a
> pair? All attempts fail because copy constructor is disallowed.
>
> Thank you very much


Without it being copyable, you can't. The best you could do be to make them
test*, or some smart pointers. That may work for you, it may not, it
depends on what you need to do with the pair.


 
Reply With Quote
 
Rob Mann
Guest
Posts: n/a
 
      07-28-2007
In article <(E-Mail Removed). com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) says...
> now, i need to create a pair of this object
> pair<test,test>(??,??)
> what should I put in the constructor to allow me to construct such a
> pair? All attempts fail because copy constructor is disallowed.
>


As others have pointed out, STL expects your class to be copyable. You
also need a default constructor.

Is there a particular reason you don't want to allow access to the copy
constructor?

-Robb
 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      07-29-2007
On Sat, 28 Jul 2007 18:33:47 -0500, Rob Mann wrote:
>As others have pointed out, STL expects your class to be copyable. You
>also need a default constructor.


Generally, STL works only with values, not with objects.

>Is there a particular reason you don't want to allow access to the copy
>constructor?


OOP? Objects (as in OOP) are non-copyable and non-assignable. STL
obviously doesn't support OOP.


--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-29-2007
Roland Pibinger wrote:
> On Sat, 28 Jul 2007 18:33:47 -0500, Rob Mann wrote:
>> As others have pointed out, STL expects your class to be copyable.
>> You also need a default constructor.

>
> Generally, STL works only with values, not with objects.


Generally? And do you by way of stating in a contadiction, assert
that objects do not have values? Forgive my English, I may have
overcomplicated what I was trying to say.

>
>> Is there a particular reason you don't want to allow access to the
>> copy constructor?

>
> OOP? Objects (as in OOP) are non-copyable and non-assignable. STL
> obviously doesn't support OOP.


Then you most likely are trying to use a wrong tool. You need
something that doesn't provide as many paradigms as C++ does. Or,
perhaps, come up with your own library that OOP purists will find
as useful as the rest of us find the Standard library.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      07-29-2007
On Sun, 29 Jul 2007 09:45:54 -0400, "Victor Bazarov" wrote:
>Roland Pibinger wrote:
>> Generally, STL works only with values, not with objects.

>
>Generally? And do you by way of stating in a contadiction, assert
>that objects do not have values?


Stateful object-oriented and stateless value-oriented concepts
represent different programming paradigms ('everything is an object'
vs. 'everything is a value'). This doesn't mean that you cannot use
both, values and objects, in the same program. If you are really
interested: B. J. MacLennan 'Values and objects in programming
languages'.

>> OOP? Objects (as in OOP) are non-copyable and non-assignable. STL
>> obviously doesn't support OOP.

>
>Then you most likely are trying to use a wrong tool. You need
>something that doesn't provide as many paradigms as C++ does.


C++ is a multiparadigm language (whatever this exactly means) but STL
is not a multiparadigm library. In order to make use of a tool you
need to understand its basic 'philosophy'. Otherwise you just have the
proverbial hammer that makes everything look like a nail.

>Or,
>perhaps, come up with your own library that OOP purists will find
>as useful as the rest of us find the Standard library.


I have no motivation to make 'OOP purists' happy. STL can be extended
to support objects (I have done this for vector) and become more
usable for everyday programming.


--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
 
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
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
Copy constructor: why can't I copy objects as if they were structs? rdc02271 C++ 24 12-27-2005 12:38 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM



Advertisments