T
TK
Hi,
is this the right way to write a copy ctor; especially this line:
// copy ctor
B(const B& obj, string c) : obj(c) {
--- my source code ---------------------------------
class A {
public:
A(string a) : s(a) { cout << "ctor A::A()" << endl; }
~A() { cout << "dtor A::A()" << endl; }
private:
string s;
};
//-----------------------------------
class B {
public:
// ctor
B(string a, char *b, string c) : s(a), s1(b), obj(c) {
cout << "ctor B::B()" << endl;
}
// copy ctor
B(const B& obj, string c) : obj(c) {
s = obj.s;
s1 = obj.s1;
}
~B() { cout << "dtor B::B()" << endl; }
B& B:
perator=(const B& b) {
if (this == &b) {
return *this;
} else {
s = b.s;
s1 = b.s1;
}
}
private:
string s;
char *s1;
// a class
A obj;
};
is this the right way to write a copy ctor; especially this line:
// copy ctor
B(const B& obj, string c) : obj(c) {
--- my source code ---------------------------------
class A {
public:
A(string a) : s(a) { cout << "ctor A::A()" << endl; }
~A() { cout << "dtor A::A()" << endl; }
private:
string s;
};
//-----------------------------------
class B {
public:
// ctor
B(string a, char *b, string c) : s(a), s1(b), obj(c) {
cout << "ctor B::B()" << endl;
}
// copy ctor
B(const B& obj, string c) : obj(c) {
s = obj.s;
s1 = obj.s1;
}
~B() { cout << "dtor B::B()" << endl; }
B& B:
if (this == &b) {
return *this;
} else {
s = b.s;
s1 = b.s1;
}
}
private:
string s;
char *s1;
// a class
A obj;
};