Learning Curve

Alanf777_Lcd_fig08

http://en.wikipedia.org/wiki/Learning_curve

When you start learning something without any prior knowledge, the learning curve must be very steep.

#include <vector>
#include "Myclass.h"

int main() {
	vector<Myclass> orange(2);
	for(auto x : orange) {
		x.display();
	}
	return 0;
}

So how many times do you expect to have your constructor and destructor called respectively?

constructor. 
constructor. 
  t1 = 1
  t2 = mystring
destructor. 
  t1 = 1
  t2 = mystring
destructor. 
destructor. 
destructor. 

Looks funny? The constructor is called twice, while the destructor is called four times.

Myclass:: Myclass(const Myclass &obj) {
	cout << "copy constructor. \n";
	t1 = obj.t1;
	t2 = obj.t2;
}

If you add a user defined copy constructor, you will know that the destructor is called as many times as the constructors.

constructor. 
constructor. 
copy constructor. 
  t1 = 1
  t2 = mystring
destructor. 
copy constructor. 
  t1 = 1
  t2 = mystring
destructor. 
destructor. 
destructor. 

So far, so good. I am learning quickly as is expected. However, I have a question. Why my learning curve is so shallow with my CW?

Leave a Reply

Your email address will not be published. Required fields are marked *