Thread: Shallow/Deep copying, pointers

  1. #1
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68

    Shallow/Deep copying, pointers

    Eshk. I have fear of the copying.....

    Basically, im not even sure my problem has anything to to with shallow/deep copying. In short, i have an object with a constructor that takes variables. What i am trying to do is make a new object, then push it onto a vector (allquestions).

    Thing is, im not sure whether Tempquestion will exist after function return. Not cool, hey? I imagine that if it does not exist after return, i will get some wonderful runtime errors to dance around in.... me thinks not.

    Code:
    Question  TempQuestion ( NumberX, NumberY,  cOperator, Answer);
    	AllQuestions.push_back ( TempQuestion );
    I thought about it for a while, and thought : can you push and construct at the same time?

    something like:
    Code:
    vector <foo> FooVector;
    FooVector.push_back ( some arguments for the constructor )
    my other problem is making a deque that hold pointers to Questions. whenever i write :
    Code:
     deque < * Question > MisAnsweredQuestions;
    My compiler tells me i have a parse error before the > (ie, i have screwed up the type).

    If any one needs more source....
    Ph33r the sphericalCUBE ™

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Thing is, im not sure whether Tempquestion will exist after function return
    Donīt have to worry about that. Every element is copied into the vector. This means that the copy-constructor is invoked. Make sure that the copyconstructor is implemeted correctly .

    I thought about it for a while, and thought : can you push and construct at the same time?
    Like this
    Code:
    AllQuestions.push_back(TempQuestion( NumberX, NumberY,  cOperator, Answer));
    Why not?

    my other problem is making a deque that hold pointers to Questions. whenever i write :
    Silly problem
    Code:
    deque <Question*> MisAnsweredQuestions;
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Basically, im not even sure my problem has anything to to with shallow/deep copying.
    Shallow copying is when you have pointer variables and you copy just the pointers, not the data that they point to. Deep copying copies the data as well so you end up with two copies of the data instead of two references to a single copy of the data. Well written copy constructors will solve this problem quite handily.

    >What i am trying to do is make a new object, then push it onto a vector (allquestions).
    Simple:
    Code:
    myvector.push_back ( myclass ( args ) );
    This creates a new object and pushes it onto the back of a vector. It saves you from having to actually create a temporary object that you would only use to push onto the vector. If the vector holds pointers to objects, you can also do this:
    Code:
    myvector.push_back ( new myclass ( args ) );
    My best code is written with the delete key.

  4. #4
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    I problably shouldn't program when i have sheep jumping across my field of vision, crying "zzzzzz! zzzzzzz!". Well, i have to because of timeconstraints, but anyway, when youre on about four hours sleep, you tend to miss things like the difference between m and n and which side of the type your pointer asterisk is on. Thanks for telling about what deep/shallow really is, too, prelude. didnt know you could push using new
    Ph33r the sphericalCUBE ™

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copying pointers
    By Tom Bombadil in forum C Programming
    Replies: 10
    Last Post: 05-22-2009, 01:26 PM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Copying pointers in structures instead of the structure data?
    By Sparrowhawk in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2009, 06:04 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM