复制构造函数问题 [英] copy constructor problem
问题描述
班级考试{
public:
test()
:a(0)
{
}
int get(){return a; }
test(const test& t)
:a(t.get())
{
}
void modify()
{
a ++;
}
private :
int a;
};
直观地看来复制构造函数的参数应该是
const测试&
但是,如果我尝试使用get()方法,我的编译器会给出错误:
i:\Documents and Settings \ Al Cpwn \ My Documents\Visual Studio
Projects\c ++ \\\。cpp(11):错误C2662:''test :: get'':无法转换
''this''从'const test''指向''test&''
有人可以帮我理解为什么吗?
class test {
public:
test()
:a(0)
{
}
int get() { return a; }
test(const test& t)
:a(t.get())
{
}
void modify()
{
a++;
}
private:
int a;
};
Intuitively it seems that the parameter to copy constructor should be
const test&
However, if I try to use get() method, my compiler gives the error:
i:\Documents and Settings\Al Cpwn\My Documents\Visual Studio
Projects\c++\1.cpp(11) : error C2662: ''test::get'' : cannot convert
''this'' pointer from ''const test'' to ''test &''
Can someone please help me understand why?
推荐答案
al.c ... @ gmail.com写道:
al.c...@gmail.com wrote:
班级测试{
公开:
test()
:a(0)
{
}
int get(){return a; }
class test {
public:
test()
:a(0)
{
}
int get() { return a; }
修改上面的行,使得get()是一个const成员函数
int get()const {return a; }
Modify the above line so that get() is a const member function
int get() const { return a; }
在消息< 11 ********************** @ j33g2000cwa中。 googlegroups .com>,
al*****@gmail.com 写道
In message <11**********************@j33g2000cwa.googlegroups .com>,
al*****@gmail.com writes
班级考试{
公开:
考试()
:a(0)
{
}
int get(){return一个; }
test(const test& t)
:a(t.get())
{
}
void modify()
{
int a;
};
直观地看来复制构造函数的参数应该是
const test&
但是,如果我尝试使用get()方法,我的编译器会给出错误:
i:\Documents and Settings \ Al Cpwn \ My Documents \\ \\ Visual Studio
Projects\c ++ \\\ cpp(11):错误C2662:''test :: get'':无法转换
''''指针来自''const test' 'to''test&''
有人可以帮我理解为什么吗?
class test {
public:
test()
:a(0)
{
}
int get() { return a; }
test(const test& t)
:a(t.get())
{
}
void modify()
{
a++;
}
private:
int a;
};
Intuitively it seems that the parameter to copy constructor should be
const test&
However, if I try to use get() method, my compiler gives the error:
i:\Documents and Settings\Al Cpwn\My Documents\Visual Studio
Projects\c++\1.cpp(11) : error C2662: ''test::get'' : cannot convert
''this'' pointer from ''const test'' to ''test &''
Can someone please help me understand why?
你试图打电话给非-const函数get()on const
引用参数t。
将其更改为int get()const {return a; }
-
Richard Herring
You''re trying to call the non-const function get() on the const
reference argument t.
Change it to int get() const { return a; }
--
Richard Herring
al ***** @ gmail.com 写道:
al*****@gmail.com wrote:
class test {
public:
test()
:a(0)
{
}
int get(){return a; }
尝试:
int get()const {return a; }
test(const test& t)
:a(t.get())
{
}
void modify()
{
a ++;
}
私有:
int a;
};
直观地看来复制构造函数的参数应该是是
const test&
但是,如果我尝试使用get()方法,我的编译器会给出错误:
class test {
public:
test()
:a(0)
{
}
int get() { return a; } Try:
int get () const { return a; }
test(const test& t)
:a(t.get())
{
}
void modify()
{
a++;
}
private:
int a;
};
Intuitively it seems that the parameter to copy constructor should be
const test&
However, if I try to use get() method, my compiler gives the error:
Best
>
Kai-Uwe Bux
Best
Kai-Uwe Bux
这篇关于复制构造函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!