vector storing inherited classes

Pages: 12
But I'n not lossing any pointer if I do something like that.
You just mean i should delete the objects before i exit the program?
Ok, I got it. So I have to delete the objects at the end and aso declare virtual destructor for the base so when deleting the inherited class to delete the base also.

I changed to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <vector>
using namespace std;

class base{
public:
   virtual void view()=0;
   virtual void store(int){}
   virtual void store(int, int){}
   virtual ~base(){cout << "Base deleted\n";}
};

class derived1 : public base{
   int num;
public:
   ~derived1(){cout << "Derived1 deleted\n";}
   void view(){
      cout << num << '\n';
   }
   void store(int i){
      num = i;
   }
};

class derived2 : public base{
   int num;
   int num2;
public:
   ~derived2(){cout << "Derived2 deleted\n";}
   void view(){
      cout << num << ',' << num2 <<'\n';
   }
   void store(int i, int x){
      num = i;
      num2 = x;
   }
};

int main(){
   vector<base *> viktor;
   //push data
   viktor.push_back(new derived1());
   viktor[0]->store(4);
   viktor.push_back(new derived1());
   viktor[1]->store(6);
   viktor.push_back(new derived2());
   viktor[2]->store(3,1);
   viktor.push_back(new derived2());
   viktor[3]->store(7,9);
   //display
   for(unsigned i=0; i<viktor.size(); i++){
      viktor[i]->view();
   }
   for(unsigned i=0; i<viktor.size(); i++){
	   delete viktor[i];
   }

}


Thanx
Oh, ok I got it.
Thanx for your help and your examples.
I really appreciate it.
Last edited on
OK, here let me add a couple of sents.
When I create a vector, I would leave the deletion/destruction of vector to vector itself, meaning, I would use one of its clear() or erase() functions, rather than delete.
I would not recommend to use 'delete' in this case.

And another thing, it is "not" a memory leak in this as the vector will be destroyed any way once the main() functionality is done. Since it is end of the main(), the vector will be gone anyway without a memory leak.

It is called "memory leak" only when the program is still running and the (reserved) memory has run out. Above that is not the case.

Check it out. Good luck :)
Last edited on
I would not recommend to use 'delete' in this case.

The 'delete' doesn't erase the vector, it just free the heap memory!
vector is the stack memory,the OS will free that!

It is called "memory leak" only when the program is still running and the (reserved) memory has run out. Above that is not the case.



Yes,It is called "memory leak" only when the program is still running and the (reserved) memory has run out.
But the heap memory had not freed is mean that "memory leak"!
When the programm is run, the os can't collect the memory while you ‘new’!
To Mitsakos:
The code is ugly!
1
2
3
4
5
6
 for(unsigned i=0; i<viktor.size(); i++){
      viktor[i]->view();
   }
   for(unsigned i=0; i<viktor.size(); i++){
	   delete viktor[i];
   }
To simo110:
You mean that i have two times the same loop?
This code is just for me to understand how it works, as a general example. Usually if i want to do something like that i just use one for loop
1
2
3
4
for(unsigned i=0; i<viktor.size(); i++){
   viktor[i]->view();
   delete viktor[i];
}

If you mean something different explain to me so I can correct it.

To satm2008:
In my actuall program I deleted the pointers and erased the vector.

Thanx for your advices
To Mitsakos :
First,
for(unsigned i=0; i<viktor.size(); i++){
viktor[i]->view();
delete viktor[i];
}


unsigned will be a force-conversion(unsigned --> vector<base *>::size_type),also the unsigned is not safe relative to the size_type.

Second,
for(unsigned i=0; i<viktor.size(); i++)

is the C++ Array--Style,not the STL-Style,Why not use the iterator?
Yes, the 'delete' is needed.
Sorry I just overlooked it. Since it is a vector of "new"ly created pointers in the program, the "delete" is right to deallocate the memory.

However, after viewing the vector, the program finishes any way so the no heap memory loss as the program goes out of running scope and the memory reserved (whatever) for program is given to others by OS, anyway.
Unless it is in another called function and the program is still running, then only it is a memory leak.
In the above given code example, it would not leak any memory.
In the above given code example, it would not leak any memory.

Yes, in this program, while this program end,the heap memory will collected by the os!
But if after this code:
1
2
3
4
  //display
   for(unsigned i=0; i<viktor.size(); i++){
      viktor[i]->view();
   }


,the program still have many code, the is many memory is loss.Until the program is closed,the memory will be collected!
Sometimes a program will run for 365*24 hours!So the program will closed for the memory will be exhausted!
In a word,the style of writing the code is not good!
I didn't know that it is advantage to use iterator instead of unsigned. If i just use int then the compiler has a warning that it converts int to unsigned. If i use unsigned there is no warning or anything.

When I used it in my program I had the delete at the end because I need the data all the way in my program. If I wanted to delete something from the vector first i delete the object and then erase it from the vector.
To Mitsakos :
You can read the book << The C++ Standard Library >> wrote by Nicolai M,Josuttis.
Yes,the size_type is a type which is similary to unsigned int!The int will be a convertes!

While the vector's element is pointer, it will be a trouble thing!You have to look after your [pointer momently!
I started reading <<The C++ Programming Language 3rd.Ed>> by Bjarne Stroustrup now. It has many things that can help me with the STL, the possibilities of C++ in general and also with my programming style.

Thank you very much.
o(∩_∩)o...
<<The C++ Programming Language 3rd.Ed>> is just a cookbook.It can't shape your programming style.
You can read the <<C++ Primer 4th>> or <<Effective C++>>!
It just has some techniques in there that can help me out. Thank you for your suggestions.
I'll see if i can find them.
Topic archived. No new replies allowed.
Pages: 12