Use reference instead of pointer

This commit is contained in:
Inex Code 2020-09-11 08:53:27 +00:00
parent eeb0cdd9f0
commit 12724d8307
1 changed files with 12 additions and 15 deletions

View File

@ -52,7 +52,6 @@ public:
void show()
{ // Должен выдавать на экран изображение при помощи printf или std::cout
// TODO реализовать
for (uint y = 0; y < my; y++)
{
for (uint x = 0; x < mx; x++)
@ -66,8 +65,6 @@ public:
int get(uint x, uint y)
{
// TODO реализовать логику
return data[mx * (y) + (x)];
}
@ -82,34 +79,33 @@ public:
throw(y);
}
data[mx * (y) + (x)] = color;
// TODO реализовать логику
}
void copy(Image *original)
void copy(Image &original)
{
freemem();
mx = original->getMx();
my = original->getMy();
mx = original.getMx();
my = original.getMy();
data = (uint *)malloc(sizeof(uint) * mx * my);
for (uint y = 0; y < my; y++)
{
for (uint x = 0; x < mx; x++)
{
data[mx * y + x] = original->get(x, y);
data[mx * y + x] = original.get(x, y);
}
}
}
// Returns true if equal
bool compare(Image *original)
bool compare(Image &original)
{
if (original->getMx() == mx && original->getMy() == my)
if (original.getMx() == mx && original.getMy() == my)
{
for (uint y = 0; y < my; y++)
{
for (uint x = 0; x < mx; x++)
{
if (data[mx * y + x] != original->get(x, y))
if (data[mx * y + x] != original.get(x, y))
{
cout << "Not equal: x = " << x << "; y = " << y << "\n";
return false;
@ -120,7 +116,7 @@ public:
}
else
{
cout << "original size: " << original->getMx() << "x" << original->getMy() << "\n";
cout << "original size: " << original.getMx() << "x" << original.getMy() << "\n";
cout << "current size: " << mx << "x" << my << "\n";
return false;
}
@ -181,7 +177,6 @@ public:
readfile.open(filename);
if (readfile.is_open())
{
// TODO
try
{
getline(readfile, line);
@ -221,8 +216,8 @@ int main()
a.show();
cout << "Image b \n";
b.show();
b.copy(&a);
if (b.compare(&a) == true)
b.copy(a);
if (b.compare(a) == true)
{
cout << "a == b after copy\n";
}
@ -247,5 +242,7 @@ int main()
a.read_from_file("image");
cout << "Image a, read from file \n";
a.show();
a.freemem();
b.freemem();
return 0;
}