Creating a pair of objects with no copy constructor?

Discussion in 'C++' started by responsible, Jul 20, 2007.

  1. responsible

    responsible Guest

    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
     
    responsible, Jul 20, 2007
    #1
    1. Advertising

  2. 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::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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 20, 2007
    #2
    1. Advertising

  3. 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::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.

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jul 20, 2007
    #3
  4. 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::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.


    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
     
    Victor Bazarov, Jul 20, 2007
    #4
  5. responsible

    Jim Langston Guest

    "responsible" <> wrote in message
    news:...
    > 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.
     
    Jim Langston, Jul 21, 2007
    #5
  6. responsible

    Rob Mann Guest

    In article <>,
    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
     
    Rob Mann, Jul 29, 2007
    #6
  7. 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
     
    Roland Pibinger, Jul 29, 2007
    #7
  8. 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
     
    Victor Bazarov, Jul 29, 2007
    #8
  9. 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
     
    Roland Pibinger, Jul 29, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aire
    Replies:
    3
    Views:
    471
    Mike Wahler
    Jan 25, 2004
  2. rdc02271
    Replies:
    24
    Views:
    743
    Jacek Dziedzic
    Dec 27, 2005
  3. ali
    Replies:
    4
    Views:
    582
    David Harmon
    Mar 5, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,253
  5. cinsk
    Replies:
    35
    Views:
    2,619
    James Kanze
    Oct 11, 2010
Loading...

Share This Page