xml*_*lmx 8 c++ naming-conventions
#include <iostream>
#include <vector>
using namespace std;
typedef int UINT4;
class Hack
{};
Hack& operator <(Hack& a , Hack& b)
{
std::cerr << "1";
return a;
}
Hack& operator >(Hack& a, Hack& b)
{
std::cerr << "2";
return a;
}
int main()
{
Hack vector; // It SHOULD be OK!?
Hack UINT4; // It SHOULD be OK!?
Hack v;
Hack Hack; // It SHOULD be OK!?
Hack = v; // It SHOULD be OK!?
// Please stop to think for a moment: What will the following output?
vector<UINT4> v;
// Oh, my god! The whole world goes mad!
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有没有听说过以下?请仔细查看参数类型:)
int stat(const char *path, struct stat *buf);
Run Code Online (Sandbox Code Playgroud)
你在问题中展示的内容真的不太戏剧化.您在本地范围中声明变量名称,但程序中的类型名称在全局范围内.我现在会告诉你一些真正的变态:
int nooo = 0;
int main() {
int nooo = 0;
}
Run Code Online (Sandbox Code Playgroud)
W00t?为什么C++允许我们创建两个同名变量!?!
好吧,我在开玩笑.我现在将向您介绍弄乱重复名称的真正黑暗面.考虑一下C++允许我们做什么!
struct A {
int A; // noes!
};
A A; // another A!
Run Code Online (Sandbox Code Playgroud)
从本质上讲,这一个是C兼容性:)
你从问题的角度提问,或者非常清楚他正在使用类名作为变量标识符的人:显然,这不是最佳实践.
现在,让我们反过来说:假设你不知道typedef int i;你所包含的一些晦涩的头文件中有一个奇怪的东西.你期望它打破以下无辜的代码吗?
int i = 0;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2331 次 |
| 最近记录: |