C++11 新特性之Class

       本文是C++11新特性介绍的第六部分,涉及到Class的相关内容。

sizeof

       新标准中,sizeof可以直接用于获取Class::member的大小,而不用通过Class的实例。

1
2
3
4
5
6
7
8
9
class TestClass
{
public:
int member_i;
char member_c;
};
cout<<"test sizeof class member:\n";
cout<<sizeof(TestClass::member_i)<<"\t"<<sizeof(TestClass::member_c)<<endl;

default constructor

       新标准中,可以通过=default强制编译器生成一个默认constructor。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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:\n";
TestClass tc; // may cause error if no default construct.
cout<<tc.member_i<<'\t'<<(short)tc.member_c<<endl;
cout<<"test =default done."<<endl;

       在上面的代码中,如果我们不提供默认constructor的话,无法通过TestClass tc定义一个实例。

allocator.construct

       新标准中,allocator.construct可以使用任意的构造函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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:\n";
allocator<TestClass> alloc;
auto p = alloc.allocate(10);
alloc.construct(p, 10);
cout<<p->member_i<<'\t'<<p->member_c<<endl;

copy constructor

       新标准中,可以通过=default要求编译器合成默认的拷贝/赋值构造函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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:\n";
TestClass tc3(tc2);
TestClass tc4 = tc2;
cout<<tc3.member_i<<'\t'<<tc3.member_c<<endl;
cout<<tc4.member_i<<'\t'<<tc4.member_c<<endl;

       同样,新标准中也允许用=delete禁止拷贝。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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:\n";
//TestClass tc5(tc2); // error: use of deleted function ‘TestClass::TestClass(const TestClass&)’
//cout<<tc5.member_i<<'\t'<<tc5.member_c<<endl;
cout<<"test =delete done."<<endl;

override 和 final

       新标准中提供了override和final两个关键字,用于标识子类对父类中虚函数的重写(override)或禁止重写(final)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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<<'\t'<<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<<'\t'<<"c: "<<member_c<<endl;
}
cout<<"test override:\n";
TestClass *stc_ptr = new SubTestClass(10);
stc_ptr->print_msg();
SubTestClass stc(10);
TestClass tc6 = (TestClass)stc;
tc6.print_msg();

委托父类构造函数

       新标准中,也支持子类在初始化列表中直接委托父类的构造函数完成初始化。

1
2
3
4
5
6
7
8
9
10
11
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:\n";
SubTestClass stc2(1024, 'H');
stc2.print_msg();

多继承与默认 constructor

       多重继承的子类可以直接继承父类的构造函数,但是如果父类中有形参列表完全相同的构造函数,则会产生冲突,这时需要子类自己定义一个自己版本的构造函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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:\n";
MultiSubClass mtc(1024);
mtc.print_msg();
return 0;

总结

       1.sizeof可以直接用于获取Class::member的大小,而不用通过Class的实例。
       2.可以通过=default强制编译器生成一个默认constructor。
       3.可以在初始化列表中将一个constructor初始化的工作委托给另一个constructor,以及父类的constructor。
       4.allocator.construct可以使用任意的构造函数。
       5.可以通过=default要求编译器合成默认的拷贝/赋值构造函数,也可以通过=delete禁止拷贝。
       6.新标准中提供了override和final两个关键字,用于标识子类对父类中虚函数的重写(override)或禁止重写(final),编译会对这两种情况进行检查。final还可以用于类的标识,表示禁止继承。
       7.多重继承的子类可以直接继承父类的构造函数,但是如果父类中有形参列表完全相同的构造函数,则会产生冲突,这时需要子类自己定义一个自己版本的构造函数。

文章目录
  1. 1. sizeof
  2. 2. default constructor
  3. 3. allocator.construct
  4. 4. copy constructor
  5. 5. override 和 final
  6. 6. 委托父类构造函数
  7. 7. 多继承与默认 constructor
  8. 8. 总结