FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Notes/cpp/effectivecpp/include/BaseItem.h at master · memorywalker/Notes · GitHub
memorywalker
Notes
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Notes
/
cpp
/
effectivecpp
/
include
/
BaseItem.h
Copy path
More file actions
More file actions
Latest commit
History
History
History
70 lines (58 loc) · 1.83 KB
Breadcrumbs
Notes
/
cpp
/
effectivecpp
/
include
/
BaseItem.h
Copy path
File metadata and controls
70 lines (58 loc) · 1.83 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#
ifndef
BASEITEM_H
#
define
BASEITEM_H
const
char
*
const
FileName =
"
BaseItem.h
"
;
const
std::string
FileExtName
(
"
.h
"
);
extern
const
int
MAX_ITEM_COUNT
;
class
UnCopyable
{
protected:
UnCopyable
() {}
//
允许派生类构造和析构
~UnCopyable
() {}
private:
UnCopyable
(
const
UnCopyable&);
//
只是声明一个私有的拷贝构造,避免子类中可以拷贝
UnCopyable&
operator
=(
const
UnCopyable&);
};
class
UniqueClass
:
private
UnCopyable
{
//
子类中不需要再声明私有的拷贝构造或拷贝赋值函数,
//
因为编译器自动生成的版本会调用基类的对应私有函数而导致编译失败
};
class
BaseItem
{
public:
BaseItem
();
//
explicit BaseItem(int id);
BaseItem
(
int
id);
virtual
~BaseItem
();
BaseItem
(
const
BaseItem& other);
BaseItem&
operator
=(
const
BaseItem& other);
const
unsigned
int
&
GetID
()
const
{
return
m_nID; }
unsigned
int
&
GetID
()
{
return
const_cast
<
unsigned
int
&>(
//
将返回值转为non-const
static_cast
<
const
BaseItem&>(*
this
).
GetID
()
//
将引用this转换为const类型,才能调用const成员函数
);
}
void
SetID
(
unsigned
int
val) { m_nID = val; }
char
*
GetName
() {
return
m_szName; }
void
SetName
(
char
* val);
void
Print
();
protected:
private:
static
const
int
MAX_LEN
=
20
;
//
static const float PIE = 3.14; // error, 不能在类内初始化静态成员
static
const
float
PIE
;
unsigned
int
m_nID;
char
m_szName[
MAX_LEN
];
};
class
Game
:
public
BaseItem
{
public:
Game
();
~Game
();
Game
(
const
Game& rhs);
Game&
operator
=(
const
Game& rhs);
private:
int
type;
};
#
endif
//
BASEITEM_H
Back
|
FazBrowse Home
|
New Git URL