Thread: Question About Scope

  1. #1
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61

    Question About Scope

    As a C programmer doing C++ stuff a few things are still confusing to me. One is scope. Consider the following code

    Code:
    #include <vector>
    using std::vector;
    
    class A_Class{
     public:
      A_Class(int x);
    // some stuff
    };
    
    void foo(vector<A_Class> *v){
    Class c(42);
    v->push_back(c);
    }
    
    int main(){
     vector<A_Class> *v = new vector<A_Class>();
     foo(v);
     return 0;
    }
    What happens to c and what happens to the vector when foo exits? std::vector<T>.push_back takes as an argument a T&, so my understanding is that the vector contains a reference to c, which is located on foo's stack. When foo returns, c should get nuked, which should screw up the vector. Is that right?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    c is copied into an instance that lives in v. It isn't a reference that lives in v
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    std::vector<T>.push_back takes as an argument a T&
    No, const T&. push_back() stores a copy of the argument.
    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

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    the vector is leaked also, you really don't need to allocate it dynamically. I do realize that's outside the "scope" of the question.... thank you, thank you. I'll be here all week.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Also, just fyi about the reference, const T&, it's there to avoid having copy the argument twice, especially if it's a large one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scope Related question
    By csonx_p in forum C++ Programming
    Replies: 13
    Last Post: 08-24-2008, 11:02 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM