Still battling with Copy Control

This is a discussion on Still battling with Copy Control within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble using push_back() on a vector of user defined types. Copy Control is a fascinating issue, but quiet ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412

    Still battling with Copy Control

    I'm having trouble using push_back() on a vector of user defined types. Copy Control is a fascinating issue, but quiet difficult to master.

    The class implements, among other things, a constructor taking a reference to a string, a copy constructor and two overloaded assignment operators.

    Code:
    aClass(const std::string& = "n/a");
    aClass(const aClass&);
    aClass& operator=(const aClass&);
    aClass& operator=(const std::string&);
    I then go on to create the vector in main()

    Code:
    vector<aClass> vec;
    vec.insert(vec.end(), "Luigi"); //atempt with insert
    vec.push_back("Anna"); //attempt with push_back
    I was under the impression that push_back would operate by first creating a new element, then a temporary of aClass using the string constructor, and then use the copy constructor to copy this temporary to the new element.

    How can I effectively do this, short of having to define a aClass object to pass as an argument to push_back()?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Hmm... some more important information...

    The class only implements one data member. A string

    The error states, on both push_backs, it cannot convert from const char to const aClass.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    I'd write a conversion method (so that you can convert strings to your type) and see if that helps. Because otherwise, you have to construct the object you want to insert first and then use push_back on it.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    One possibility is to have a constructor that accepts a const char*
    Another way would be to simply write:
    Code:
    vec.push_back(std::string("Anna"));
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    *shrugs*
    Code:
    class aClass {
        public:
            /* ... */
            operator std::string() { return myString; }
            /* ... */
    };

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    I dont think that will work, Desolation. Mario F. wants to have a const char[5] or const char[6] convertible to aClass, not to have aClass convertible to std::string. From what I see, making const char* convertible to aClass by means of a constructor that accepts a const char* as an argument is one way.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Did he try:
    Code:
    vector< aClass > vec;
    aClass ac("Anna");
    vec.push_back(ac);

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    hmm... perhaps more likely:
    Code:
    vec.push_back(aClass("Anna"));
    Return value optimisation applies here, right?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    It works. Thanks for the tips Citizen and Laserlight.

    Converting the literal to a string was what I needed. I keep forgetting the difference between a string literal (const char) and a string. The error was telling me exactly that, but I wasn't looking. I did like the aClass() approach too. Probably easier to read since it doesn't trick the reader into thinking the vector contains strings.

    But I did find one interesting thing... insert() is faster than push_back()
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Actually I'm wrong. It isn't. It's slower. And also found the importance of reserve().

    Every constructor, operator overload and the destructor has a cout inside letting me know when it is being called. This is a toy class I'm building as I learn about copy control.

    On my code above I had insert() being called once and push_back being called next.

    Code:
    vector<aClass> vec;
    vec.insert(vec.end(), aClass("Luigi"));
    vec.push_back(aClass("Luigi"));
    The output was...
    *inserting an element in vector...
    ...inside string constructor...
    ...inside copy constructor...
    ...inside copy constructor...
    ...inside destructor...
    ...inside destructor...
    *inserting another element in vector...
    ...inside string constructor...
    ...inside copy constructor...
    ...inside copy constructor...
    ...inside copy constructor...
    ...inside destructor...
    ...inside destructor...
    ...inside destructor...
    It seemed insert() was faster. But then I swapped push_back() with insert() and now push-back was faster. It didn't make sense untill I remembered how vectors allocate memory. All those calls are happening because the vector is being moved in memory. Once I reserved some memory for it, it looked much cleaner. And also taught me insert() is slower.

    *inserting an element in vector... (with insert)
    ...inside string constructor...
    ...inside copy constructor...
    ...inside copy constructor...
    ...inside destructor...
    ...inside destructor...
    *inserting another element in vector... (with push_back)
    ...inside string constructor...
    ...inside copy constructor...
    ...inside destructor...
    Last edited by Mario F.; 06-23-2006 at 08:27 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to copy a C struct to a C++ class?
    By Ptbamboo in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2009, 01:11 PM
  2. Copying constant amount of data
    By TriKri in forum C++ Programming
    Replies: 16
    Last Post: 07-12-2008, 06:32 AM
  3. (Multiline) Edit Control Limit
    By P4R4N01D in forum Windows Programming
    Replies: 9
    Last Post: 05-17-2008, 11:56 AM
  4. line number on a rich edit control
    By rakan in forum Windows Programming
    Replies: 1
    Last Post: 02-18-2008, 06:58 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21