#include <iostream>
class Uncopyable {
public:
Uncopyable();
~Uncopyable() {}
Uncopyable(const Uncopyable&);
Uncopyable& operator=(const Uncopyable&);
};
Uncopyable::Uncopyable() {
std::cout << "inherit default constructor\n";
}
Uncopyable::Uncopyable(const Uncopyable&) {
std::cout << "inherit copy constructor\n";
}
Uncopyable& Uncopyable::operator=(const Uncopyable&) {
std::cout << "inherit copy asigment\n";
return *this;
}
class HomeForSale: private Uncopyable {
};
int main() {
HomeForSale s1;
HomeForSale s2(s1);
}
複製代碼
輸出ios
inherit default constructor
inherit copy constructor
複製代碼
子類調用相應的父類構造函數(counterpart)bash
#include <iostream>
class Uncopyable {
public:
Uncopyable();
~Uncopyable() {}
Uncopyable(const Uncopyable&);
Uncopyable& operator=(const Uncopyable&);
};
Uncopyable::Uncopyable() {
std::cout << "inherit default constructor\n";
}
Uncopyable::Uncopyable(const Uncopyable&) {
std::cout << "inherit copy constructor\n";
}
Uncopyable& Uncopyable::operator=(const Uncopyable&) {
std::cout << "inherit copy asigment\n";
return *this;
}
class HomeForSale: private Uncopyable {
public:
HomeForSale();
HomeForSale(const HomeForSale&);
};
HomeForSale::HomeForSale() {
std::cout << "HomeForSale default constructor\n";
}
HomeForSale::HomeForSale(const HomeForSale&) {
std::cout << "HomeForSale copy constructor\n";
}
int main() {
HomeForSale s1;
HomeForSale s2(s1);
}
複製代碼
輸出函數
inherit default constructor
HomeForSale default constructor
inherit default constructor
HomeForSale copy constructor
複製代碼
子類調用父類默認構造函數ui