Question About Scope

This is a discussion on Question About Scope within the C++ Programming forums, part of the General Programming Boards category; As a C programmer doing C++ stuff a few things are still confusing to me. One is scope. Consider the ...

  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
    21,662
    std::vector<T>.push_back takes as an argument a T&
    No, const T&. push_back() stores a copy of the argument.
    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

  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
    dra
    dra is offline
    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, 12: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, 08:22 PM

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