可能重复:
C++ 中 const 声明之间的区别
#include <iostream>
class Bar{};
void foo(const Bar x){} //l5
void foo(Bar x){} //l6
void foo(Bar const x){} //l7
////pointer functions
void foo(const Bar* x){} //l11
void foo(Bar* x){} //l12
void foo(Bar* const x){} //l13
编译器输出:(长话短说l5
,冲突l6
;l7
但只有冲突l12
)l13
untitled.cpp:6:6: error: redefinition of ‘void foo(Bar)’
untitled.cpp:5:6: error: ‘void foo(Bar)’ previously defined here
untitled.cpp:7:6: error: redefinition of ‘void foo(Bar)’
untitled.cpp:5:6: error: ‘void foo(Bar)’ previously defined here
untitled.cpp:13:6: error: redefinition of ‘void foo(Bar*)’
untitled.cpp:12:6: error: ‘void foo(Bar*)’ previously defined here
怎么了?
- 每个声明的含义是什么
- 为什么所有 3 个声明都与对象函数冲突,但只有 2 个与指针函数冲突?
l12
请详细说明and之间的冲突l13
,即使l12
不包含const
关键字- 如果是琐碎的问题,真的很抱歉