The document discusses C++ language features and implementation details. It begins with an overview of object memory layouts for simple classes, single inheritance, and virtual functions. It then covers function calling conventions and how compilers convert function calls, including virtual function calls. Finally, it discusses passing and returning objects from functions and the use of temporary objects. The document provides insights into how various C++ language features are implemented under the hood.
24. Function Object
Member Function pointer Temporal Object dynamic_cast
Function pointer
Pass By Reference Pure Virtual Class RTTI
typeid
Overloading Pass By Value Pure Virtual Function
Copy Constructor
Virtual Function Multiple Inheritance
Constructor Dynamic Binding
Inheritance
Exception
Destructor
Class Virtual Inheritance
Friend
Template
String
Adaptor
Namespace
STL
Operator New RAII Iterator Function Template Class Template
Template Specialization
Memory Management
Partial Specialization
Smart Pointer
Reference Preprocessor
Template Meta Programming
31. Timeline
2011
1980 1989 C++11
C with class CFront 2.0 1992
HP C++ Lambda Expression
Class Multiple Inheritance Automatic Type
Inheritance Abstract Class Exception Threading
1983 - 1986 1991 1996
C++ - CFront 1.1 CFront 3.0 C++ 98/03
Virtual Function Template STL
Overloading Namespace
Reference
47. C++ is deterministic
• Destructing is determinate
- No GC
• Performance is predictable
- C++ is fast only when coders are experts
- Java is almost as fast, much higher
productivity
48. Where the Focus Is
Efficiency Flexibility Abstraction Productivity
C non-goal non-goal
C++ non-goal
at the at the
Java, C#
expense of expense of
49. Conclusion
• There are only two kinds of languages
- The ones people always complain about
- The ones nobody uses
• C++ is an undoubted success
• One language doesn’t fit all
- Important to known when and when not
52. 简单对象
class Animal {
public:
void Run();
private:
char* name;
int age;
char sex;
};
53. 简单对象
class Animal { name
public:
void Run();
private: age
char* name;
int age;
char sex;
}; Sex Alignment
54. 单一继承
class Animal {
public:
void Run();
private:
char* name;
int age;
char sex;
};
class Lion : public Animal {
private:
char* address;
};
55. 单一继承
class Animal { address
public:
void Run();
private: name
char* name;
int age;
char sex;
}; age
class Lion : public Animal {
private:
char* address; Sex Alignment
};
73. 简单类型函数调用
int sum(int a, int b) {
int result = a + b;
return result;
}
int main(int argc, char* argv[])
{
sum(1, 4);
return 0;
}
* 调用栈与机器架构有 , 这里以x86为例.
74. 简单类型函数调用
...
int sum(int a, int b) {
int result = a + b; 4
return result;
}
1
int main(int argc, char* argv[])
{
sum(1, 4);
return 0;
}
* 调用栈与机器架构有 , 这里以x86为例.
75. 简单类型函数调用
...
int sum(int a, int b) {
int result = a + b; 4
return result;
}
1
int main(int argc, char* argv[])
{ Return Address
sum(1, 4);
return 0;
}
* 调用栈与机器架构有 , 这里以x86为例.
76. 简单类型函数调用
...
int sum(int a, int b) { 5
int result = a + b; eax 4
return result;
}
1
int main(int argc, char* argv[])
{ Return Address
sum(1, 4);
return 0;
} ebp Saved ebp
esp sum
* 调用栈与机器架构有 , 这里以x86为例.
98. 静态变量初始化
Animal animal("Lion"); void __sti_ii() {
animal.Animal::Animal();
int Foo() { num = Foo();
... }
}
void __std_ii() {
int num = Foo(); animal.Animal::~Animal();
}
99. inline vs. iterator
vector<int>::iterator it = vec.begin();
vector<int>::iterator end = vec.end(); for (vector<int>::iterator it = vec.begin();
for (; it != end; ++it) { it != vec.end(); ++it) {
} }
100. inline vs. iterator
vector<int>::iterator it = vec.begin();
vector<int>::iterator end = vec.end(); for (vector<int>::iterator it = vec.begin();
for (; it != end; ++it) { it != vec.end(); ++it) {
it = vec.erase(it); it = vec.erase(it);
} }
101. inline vs. iterator
vector<int>::iterator it = vec.begin();
vector<int>::iterator end = vec.end(); for (vector<int>::iterator it = vec.begin();
for (; it != end; ++it) { it != vec.end(); ++it) {
it = vec.erase(it); it = vec.erase(it);
} }
103. Prove I t!
• Only Code Never Lies
• Assembly Language
• g++
- -S
• nm
104. References
• Bjarne Stroustrup. 1994. The Design And
Evolution of C++
• Stanley B. Lippman. 1996. Inside the C++
Object Model
• Scott Meyers. 2006. Effective C++
• 陈硕. 2012. C++工程实践