Thread: Still battling with Copy Control

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    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()?
    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
    Ireland
    Posts
    8,446
    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.
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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
    28,413
    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"));
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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
    28,413
    hmm... perhaps more likely:
    Code:
    vec.push_back(aClass("Anna"));
    Return value optimisation applies here, right?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Ireland
    Posts
    8,446
    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()
    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
    Ireland
    Posts
    8,446
    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.
    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, 02: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, 07:58 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM