????????C++11???????????????????漰??Class??????????
????????toy code????????????????????o?????????
????sizeof
?????±???У?sizeof?????????????Class::member???С???????????Class???????
class TestClass
{
public:
int member_i;
char member_c;
};
cout<<"test sizeof class member: ";
cout<<sizeof(TestClass::member_i)<<" "<<sizeof(TestClass::member_c)<<endl;
????default constructor
?????±???У????????=default??????????????????constructor??
class TestClass
{
public:
TestClass() = default;
TestClass(const int i?? const char c): member_i(i)?? member_c(c) {}
int member_i;
char member_c;
};
cout<<"test =default class construct: ";
TestClass tc; // may cause error if no default construct.
cout<<tc.member_i<<' '<<(short)tc.member_c<<endl;
cout<<"test =default done."<<endl;
???????????????У??????????????constructor???????????TestClass tc????????????
????delegate constructor
?????±???У????????????б??н????constructor????????????и??????constructor??
class TestClass
{
public:
TestClass() = default;
TestClass(const int i?? const char c): member_i(i)?? member_c(c) {}
TestClass(const int i): TestClass(i?? 0) { member_c = 'T';}
int member_i;
char member_c;
};
cout<<"test delegating constructor: ";
TestClass tc2(2);
cout<<tc2.member_i<<' '<<tc2.member_c<<endl;
????allocator.construct
?????±???У?allocator.construct???????????????????
class TestClass
{
public:
TestClass() = default;
TestClass(const int i?? const char c): member_i(i)?? member_c(c) {}
TestClass(const int i): TestClass(i?? 0) { member_c = 'T';}
int member_i;
char member_c;
};
cout<<"test allocator: ";
allocator<TestClass> alloc;
auto p = alloc.allocate(10);
alloc.construct(p?? 10);
cout<<p->member_i<<' '<<p->member_c<<endl;
????copy constructor
?????±???У????????=default??????????????????/???????????
class TestClass
{
public:
TestClass() = default;
TestClass(const int i?? const char c): member_i(i)?? member_c(c) {}
TestClass(const int i): TestClass(i?? 0) { member_c = 'T';}
TestClass(const TestClass&) = default;
TestClass& operator=(const TestClass&);
int member_i;
char member_c;
};
cout<<"test =default class copy construct: ";
TestClass tc3(tc2);
TestClass tc4 = tc2;
cout<<tc3.member_i<<' '<<tc3.member_c<<endl;
cout<<tc4.member_i<<' '<<tc4.member_c<<endl;
??????????±???????????=delete?????????
class TestClass
{
public:
TestClass() = default;
TestClass(const int i?? const char c): member_i(i)?? member_c(c) {}
TestClass(const int i): TestClass(i?? 0) { member_c = 'T';}
TestClass(const TestClass&) = delete;
TestClass& operator=(const TestClass&);
int member_i;
char member_c;
};
TestClass& TestClass::operator=(const TestClass&) = default;
cout<<"test =delete class copy construct: ";
//TestClass tc5(tc2); // error: use of deleted function ‘TestClass::TestClass(const TestClass&)’
//cout<<tc5.member_i<<' '<<tc5.member_c<<endl;
cout<<"test =delete done."<<endl;
????override??final
?????±????????override??final???????????????????????????麯??????д??override????????д??final????
class TestClass
{
public:
TestClass() = default;
TestClass(const int i?? const char c): member_i(i)?? member_c(c) {}
TestClass(const int i): TestClass(i?? 0) { member_c = 'T';}
TestClass(const TestClass&) = default;
TestClass& operator=(const TestClass&);
virtual void print_msg() {cout<<member_i<<' '<<member_c<<endl;}
virtual void final_foo() final {}
int member_i;
char member_c;
};
TestClass& TestClass::operator=(const TestClass&) = default;
class SubTestClass final: public TestClass
{
public:
using TestClass::TestClass;
SubTestClass(const int i): TestClass(i?? 'S') {}
void print_msg() override;
//void print_msg(char c) override;
//‘void SubTestClass::print_msg(char)’ marked override?? but does not override
//void final_foo() {}
//overriding final function ‘virtual void TestClass::final_foo()’
};
//class SubSubTestClass: public SubTestClass {};
// cannot derive from ‘final’ base ‘SubTestClass’ in derived type ‘SubSubTestClass’
void SubTestClass::print_msg()
{
cout<<"i: "<<member_i<<' '<<"c: "<<member_c<<endl;
}
cout<<"test override: ";
TestClass *stc_ptr = new SubTestClass(10);
stc_ptr->print_msg();
SubTestClass stc(10);
TestClass tc6 = (TestClass)stc;
tc6.print_msg();
????????????override???????????????д?????е????????????final?????????????д???????????????
??????????????final????????????????????С?
??????и???????
?????±???У????????????????б????????и??????????????????
????class SubTestClass final: public TestClass
????{
????public:
????using TestClass::TestClass;
????SubTestClass(const int i): TestClass(i?? 'S') {}
????void print_msg() override;
????};
????cout<<"test inherit base class contructor: ";
????SubTestClass stc2(1024?? 'H');
????stc2.print_msg();
?????????????constructor
?????????е????????????и???????????????????????????β??б???????????????????????????????????????????????????汾?????????
class TestClass2
{
public:
TestClass2() = default;
TestClass2(const int i) {}
};
class MultiSubClass: public TestClass?? public TestClass2
{
public:
using TestClass::TestClass;
using TestClass2::TestClass2;
// conflicts with version inherited from ‘TestClass’
MultiSubClass(const int i): TestClass(i) {}
MultiSubClass() = default;
};
cout<<"test multi inherit constructor: ";
MultiSubClass mtc(1024);
mtc.print_msg();
return 0;
???????
????sizeof?????????????Class::member???С???????????Class???????
???????????=default??????????????????constructor??
???????????????б??н????constructor????????????и??????constructor??????????constructor??
????allocator.construct???????????????????
???????????=default??????????????????/???????????????????=delete?????????
?????±????????override??final???????????????????????????麯??????д??override????????д??final??????????????????????м?顣final?????????????????????????С?
?????????е????????????и???????????????????????????β??б???????????????????????????????????????????????????汾?????????