Как правильно сделать конструктор копирования в классе?
#include <iostream>
using namespace std;
class Test{
public:
Test(){
name="First";
mas=1;
get();
};
Test(string a, double b){
name=a;
mas=b;
get();
};
Test(const Test &test):
name(test.name), mas(test.mas){
cout<<"Copy constructor worked here!\n";
get();
};
private:
string name;
double mas;
void get(){
cout << "Name = " << name << "\n" << "Mas = " << mas << endl;
};
};
int main() {
Test *first = new Test();
Test *second = new Test("dadad", 12);
Test copyfirst(first);
return 0;
}
Сделал класс и 3 конструктора(по умолчанию, с параметрами и конструктор копирования) Не понимаю как правильно сделать конструктор копирования с динамическими типами данными, сверху код с моей попыткой.
Ответы (1 шт):
Автор решения: pane
→ Ссылка
int main() {
Test *first = new Test();
Test *second = new Test("dadad", 12);
Test copyfirst(*first);
Test copyfirst(first); -> Test copyfirst(*first);