Creating a pair of objects with no copy constructor?

R

responsible

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
 
V

Victor Bazarov

responsible said:
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::pair 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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

You can't. std::pair 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.
 
V

Victor Bazarov

Erik said:
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
 
J

Jim Langston

responsible said:
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.
 
R

Rob Mann

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
 
R

Roland Pibinger

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.
 
V

Victor Bazarov

Roland said:
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.
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
 
R

Roland Pibinger

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'.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top