**
class.h**
#ifndef class_h_
#define class_h_
#include <iostream>
#include <string.h>
using std::cout;
using std::endl;
class Cow {
private:
char name[20];
char hobby;
double weight;
public:
Cow();
Cow(const char nm, const char*ho, double wt);
Cow(const Cow &c);
~Cow();
Cow & operator=(const Cow & c);
void ShowCow() const;
};
#endif // !classh
** def.cpp**
#include "class.h"
Cow::Cow()
{
name[0] = '\0';
hobby = new char[1];
hobby[0] = '\0';
weight = 0;
}
Cow::Cow(const char* nm, const char*ho, double wt)
{
int len_h = strlen(ho);
int len_n = strlen(nm);
hobby = new char(len_h + 1);
strcpy_s(name, len_n +1,nm);
strcpy_s(hobby,len_h+1 ,ho);
weight = wt;
}
Cow::~Cow()
{
delete[] hobby;
}
Cow::Cow(const Cow &c) // konstruktor kopiujący
{
int len_h = strlen(c.hobby);
int len_n = strlen(c.name);
hobby = new char[len_h + 1];
strcpy_s(hobby, len_h+1, c.hobby);
strcpy_s(name, len_n+1, c.name);
weight = c.weight;
}
Cow& Cow::operator=(const Cow & c) // celem operacji jest obiekt na rzecz którego wywołujemy metode ( jest on po lewej)
{
if (this == &c)
return *this;
delete[] hobby; // obiekt docelowy może być zajęty więc zwalniamy
int len_h = strlen(c.hobby);
hobby = new char[len_h + 1];
strcpy_s(hobby,len_h+1 ,c.hobby);
int len_n = strlen(c.name);
strcpy_s(name, len_n+1 , c.name);
weight = c.weight;
return *this; // zwracamy obiekt na rzecz którego wywoływano metode
}
void Cow::ShowCow() const
{
cout << name<<endl;
cout << hobby << endl;
cout << weight << endl;
}
main.cpp
#include "class.h"
int main()
{
Cow krowa0;
Cow krowa1 = Cow("jan", "trawa", 2);
krowa1.ShowCow();
system("pause");
return 0;
}
Hejka, mam problem z głębokim kopiowaniem :( Wywala mi błąd: "CRT detected that the application wrote to memory after end of heap buffer" szukałem, ale nie doszukałem się błędu(może problem leży w strcpy_s ??). Dokładnie problem leży w składowej hobby, gdy nie próbuje jej wyświetlić wszystko działa jak należy.
Dziękuje za pomoc ;)