可能是重复问题:
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
和l13
之间的冲突,即使l12
不包含const
关键字 - 如果问题微不足道,请多多包涵。