由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 编译器如何分辨返回类型不同的函数?
相关主题
问一个简单的C++问题请问一个exception题目
[合集] 关于template和inheritance的问题请教C++里面
conversion between const to nonconst两个继承问题
namespace 问题为什么我看不懂下面的code,是不是水平还不够?
请问这个C++程序有什么问题吗C++ 弱问一个
关于C++中一个Class的大小 (转载)C++疑问
这个在c++ const不变,不能用相同地址的变量改,咋做的two c++ interview questions! (转载)
a simple question for C++ class请教一个作用域的问题
相关话题的讨论汇总
话题: dat话题: const话题: endl话题: cout话题: get
进入Programming版参与讨论
1 (共1页)
g*********s
发帖数: 1782
1
how does the compiler know if the 1st get() or the 2nd get() should be
called?
#include
using namespace std;
class X {
public:
X(int x = 0): dat(x) {}
void print() const { cout << dat << endl; }
void inc() { dat ++; }
private:
int dat;
};
class Y {
public:
Y(const X& x): dat(x) {}
Y(const Y& y) { dat = y.get(); }
X& get() { cout << "ref" < const X& get() const { cout << "const ref" << endl; return dat;
}
private:
X dat;
};
int main()
{
X x(10);
Y y(x);
Y y2(y);
y.get().print();
y.get().inc();
y.get().print();
}
P********e
发帖数: 2610
2
我觉得除非有const lvalue才会用第二个。
而且,inc()肯定只能用1st get()

【在 g*********s 的大作中提到】
: how does the compiler know if the 1st get() or the 2nd get() should be
: called?
: #include
: using namespace std;
: class X {
: public:
: X(int x = 0): dat(x) {}
: void print() const { cout << dat << endl; }
: void inc() { dat ++; }
: private:

g*********s
发帖数: 1782
3
i see. it's quite simple actually.

【在 P********e 的大作中提到】
: 我觉得除非有const lvalue才会用第二个。
: 而且,inc()肯定只能用1st get()

1 (共1页)
进入Programming版参与讨论
相关主题
请教一个作用域的问题请问这个C++程序有什么问题吗
私有成员不能用类成员函数修改?关于C++中一个Class的大小 (转载)
compare double to float这个在c++ const不变,不能用相同地址的变量改,咋做的
[合集] C++问题(copy constructor)a simple question for C++ class
问一个简单的C++问题请问一个exception题目
[合集] 关于template和inheritance的问题请教C++里面
conversion between const to nonconst两个继承问题
namespace 问题为什么我看不懂下面的code,是不是水平还不够?
相关话题的讨论汇总
话题: dat话题: const话题: endl话题: cout话题: get