- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
代码如下:
struct A {};
void f (A& a) {std::cout << "No const" << std::endl;}
void f (const A& a) {std::cout << "const" << std::endl;}
int main(){
f(A()); // indicated as line A, output will be "const".
}
问题一:
为什么 A
行的输出是“const”,即使 A()
是一个非常量对象?
我认为编译器生成的代码等同于:
const A tempA;
f(tempA);
是这样吗?
问题2),如果两个f函数如下修改,改变对值类型的引用
void f (A a) {std::cout << "No const" << std::endl;}
void f (const A a) {std::cout << "const" << std::endl;}
以上代码无法编译。编译器给出“函数 f 的重新定义”错误。我可以理解为什么编译器会给出错误,因为可以将 const A
转换为 A
,反之亦然。因此无法在编译时决定。是这样吗?
我还是很好奇c++规范里是不是定义的很好?
最佳答案
Why the line A's output is "const", even though A() is a object without const?
A()
表达式创建了一个不能被非常量左值引用绑定(bind)的临时纯右值,因此 void f (const A& a)
是唯一可行的过载。
I am still very curious that is it very well defined in c++ specification ?
§13.1 [过载]/p3:
— Parameter declarations that differ only in the presence or absence of
const
and/orvolatile
are equivalent. That is, theconst
andvolatile
type-specifiers for each parameter type are ignored when determining which function is being declared, defined, or called. [ Example:typedef const int cInt;
int f (int);
int f (const int); // redeclaration of f(int)
int f (int) { /* ... */ } // definition of f(int)
int f (cInt) { /* ... */ } // error: redefinition of f(int)— end example ]
Only the
const
andvolatile
type-specifiers at the outermost level of the parameter type specification are ignored in this fashion;const
andvolatile
type-specifiers buried within a parameter type specification are significant and can be used to distinguish overloaded function declarations. In particular, for any typeT
, “pointer toT
,” “pointer toconst T
,” and “pointer tovolatile T
” are considered distinct parameter types, as are “reference to T,” “reference toconst T
,” and “reference tovolatile T
.”
对于 A
和 const A
类型的参数,当由任何可转换为 A
的参数初始化时,相同的转换序列适用,编译器永远不会能够在两者之间进行选择。
A&
和const A&
类型的参数可以有不同的表达式,以便编译器可以选择更好的转换顺序或从候选集中排除任何候选。
关于c++ - 重载具有其他相同签名的函数以接受引用和 const 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27398049/
假设我有一个类,我在其中重载了运算符 == : Class A { ... public: bool operator== (const A &rhs) const; ... };
我知道你不应该使用 std::find(some_map.begin(), some_map.end()) 或 std::lower_bound,因为它会采用线性时间而不是 some_map.lowe
我正在尝试在 Haskell 中定义 Vector3 数据类型,并允许在其上使用 (+) 运算符。我尝试了以下方法: data Vector3 = Vector3 Double Double Doub
我已经为我的类图将运算符重载为“-”。它的用途并不完全直观(糟糕的编码 - 我知道)但是如果我做 graph3 = graph2-graph1 那么图 3 是应该只接收图 2 和图 1 中的那些顶点。
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Operator overloading 我想重载 以按字母顺序排列字符串,但我不确定该怎么做。 如何再次
下面的代码给我一个编译错误。谁能告诉我为什么? class mytype { public: int value; mytype(int a) { value = a;
这有什么问题吗? class Vec2 attr_accessor :x, :y # ... def += (v) @x += v.x @y += v.y retu
是否可以重载 [] 运算符两次?允许这样的事情:function[3][3](就像在二维数组中一样)。 如果可能的话,我想看看一些示例代码。 最佳答案 您可以重载 operator[] 以返回一个对象
我的团队目前正在与 Lua 合作,创建一个 android 游戏。我们遇到的一件事是表面上无法创建重载构造函数。 我习惯于使用默认值设置一个对象,然后在需要时使其过载。 前任: apples() {
我有一个网页,在某个时候显示一个导航栏,它只不过是一个 a 元素的列表 (ul)。所述 a 元素的大多数样式规则都是通用的。唯一应该改变的部分是要显示的图像,可以从列表中每个 li 元素的 id 标签
我对使用/重载“范围步长”运算符(.. ..)很感兴趣,但我终其一生都无法了解如何使用它。 在文档中它说 // Usage: start .. step .. finish 但是在 F# shell
Java 11(可能无关紧要): public static String toString(Object obj) { return ReflectionToStringBuilder.to
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我无法理解以下代码(针对行号进行注释) class Base { void m1(Object o) { } void m2(String o) { } } publi
我有以下代码片段: #include using namespace std; struct Integer{ int x; Integer(const int val) : x(v
class myclass{ //definitions here }; myclass e; int myarray[10]; /* Do something... */ e = myarray;
为什么不能将下标运算符(operator [])作为 friend 函数重载? 最佳答案 正如Bjarne Stroustrup在D&E book中所说: However, even in the o
我有以下代码片段: #include using namespace std; struct Integer{ int x; Integer(const int val) : x(v
因此,我有一个问题是我最近尝试重载 namespace Eng { /** * A structure to represent pixels */ typedef
如何重载onResume()以正确的方式工作?我想从 activity 返回到 MainActivity ,我希望在其中具有与应用程序启动后相同的状态。我想使用 recreate() 但它循环了或者类
我是一名优秀的程序员,十分优秀!