#include<iostream>
using namespace std;
class Time
{
private:
char *str;
int len;
static int ilosc;
public:
Time(char *ch);
Time();
~Time();
Time(Time &t);
friend ostream &operator<<(ostream &os, Time &t);
void show();
};
int Time::ilosc=0;
Time::~Time()
{
cout << str << ", skasowany" << endl;
ilosc--;
delete [] str;
cout << "Pozostalo: " << ilosc << endl;
}
Time::Time(Time &t)
{
len=t.len;
str=new char[len+1];
strcpy(str,t.str);
ilosc++;
cout << ilosc << ": " << str << ", utworzony <--- konstruktor kopiujacy " << endl;
}
Time::Time(char *ch)
{
len=strlen(ch);
str=new char[len+1];
strcpy(str,ch);
ilosc++;
cout << ilosc << ": " << str << ", utworzony " << endl;
}
Time::Time()
{
len=4;
str=new char[len];
strcpy(str,"C++");
ilosc++;
cout << ilosc << ": " << str << ", utworzony " << endl;
}
ostream &operator<<(ostream &os, Time &t)
{
os << t.str << endl;
}
void callme1(Time &t)
{
cout << "Obiekt ciagu przekazany przez referencje: " << endl;
cout << t << endl;
}
void callme2(Time t)
{
cout << "Obiekt ciagu przekazany przez wartosc: " << endl;
cout << t << endl;
}
void Time::show()
{
cout << "Ilosc obiektow: " << ilosc << endl;
}
int main(){
Time st1("siema heniu");
Time st2(":)");
Time st3(st1);
Time st4("Ucze sie c++, chce byc programista w przyszlosci");
callme2(st1); // czemu wywolywany jest konstruktor kopiujacy ????
st1.show();
cout << "\n\n" << endl;
system("pause");
return 0;
}
Dokładnie chodzi mi o linijkę
callme2(st1); // czemu wywolywany jest konstruktor kopiujacy ????
Czemu uruchamia się konstruktor kopiujący jak wywołuje tylko zwykłą funkcję