Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- struct Ball {
- int x; // поля, свойства
- int y;
- char* name;
- };
- bool isEqual(Ball b1, Ball b2)
- {
- if (b1.x != b2.x)
- return false;
- if (b1.y != b2.y)
- return false;
- if (strcmp(b1.name, b2.name) != 0)
- return false;
- return true;
- }
- void printBall(Ball ball)
- {
- cout << "X = " << ball.x << "\nY = " << ball.y << endl;
- cout << ball.name << endl;
- }
- void resetBall(Ball* pB) // ожидается адрес переменной типа Ball
- {
- cout << pB << endl;
- pB->x = 0;
- pB->y = 0;
- }
- Ball makeCopy(Ball b)
- {
- Ball temp;
- temp.x = b.x;
- temp.y = b.y;
- temp.name = new char[strlen(b.name) + 1];
- strcpy(temp.name, b.name);
- return temp;
- }
- int main()
- {
- Ball b = {0, 0};
- b.x = 200;
- b.y = 300;
- b.name = new char[100];
- strcpy(b.name, "Ball 1");
- cout << sizeof(b) << endl;
- printBall(b);
- Ball d = makeCopy(b); // копирование без выделения памяти (неглубокое копирование)
- printBall(d);
- strcpy(b.name, "New name");
- printBall(b);
- printBall(d);
- delete [] b.name;
- delete [] d.name;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement