deleting an array that stores pointers to some objects

Posted by memC on Stack Overflow See other posts from Stack Overflow or by memC
Published on 2010-05-15T08:06:34Z Indexed on 2010/05/15 8:14 UTC
Read the original article Hit count: 123

Filed under:
|

hi,

I am storing pointers to elements of a vec_A in an array A* a_ptrs[3] . Assume that vec_A will not be resized. So, a_ptrs[i] will point to the correct element.

My question is:

Suppose A* a_ptrs[3] is declared in a class B. Since it is not created using 'new' I am guessing I don't need to delete it in the destructor. Am I right??

class A {
      public:                       
             int getNumber();            
             A(int val);
             ~A(){};
      private:
              int num;
};    
A::A(int val){
         num = val;
         };             
int A::getNumber(){
    return num;
};

 int main(){  
    int i  =0;
    int num;    
    std::vector<A> vec_A;
    for ( i = 0; i < 10; i++){
        vec_A.push_back(A(i));
        }

    A* a_ptrs[3];        
    a_ptrs[0] = &vec_A[0];        
    a_ptrs[1] = &vec_A[3];        
    a_ptrs[2] = &vec_A[5];

    for (i = 0; i<3; i++){
        std::cout<<"\n: a_ptrs[i].getNumber() = "<<a_ptrs[i]->getNumber();
    }        
    std::cout << "\nPress RETURN to continue...";
    std::cin.get();    
    return 0;
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about beginner