无可变持久性 [英] No Variable Permanence
问题描述
Aieeeeaiiieeaiiieii,啊想啊需要一些亲zac
出于某种原因,我的对象变量并没有将它们的值保持在
之外方法范围。例如,给定:
//开始代码
class Parent {
public:
Parent(){ } $ / $
virtual~Parent(){}
virtual void setSomething(int thing){}
virtual int getSomething(){return 0}
...
};
class孩子:公共父母{
public:
Child(){...}
Child(int thing){
...
setSomething(thing); < br $>
}
~Child(){...}
void setSomething(int thing){
something =东西;
}
int getSomething(){
返回东西;
}
private:
int something;
};
int main(){
孩子* c;
c =新孩子(12);
cout<< c.getSomething()<< endl;
返回0;
}
//结束代码
>
输出不是12,而是-842150451。我怀疑我正在做虚拟关键字的错误。谁能解释一下呢?
谢谢,
拉里
Aieeeeaiiieeaiiieii, ah think ah need some pro-zac
For some reason, my object variables aren''t keeping their values outside of
the method scope. For instance, given:
//Begin code
class Parent{
public:
Parent(){}
virtual ~Parent(){}
virtual void setSomething(int thing){}
virtual int getSomething(){return 0}
...
};
class Child: public Parent{
public:
Child(){...}
Child(int thing){
...
setSomething(thing);
}
~Child(){...}
void setSomething(int thing){
something=thing;
}
int getSomething(){
return something;
}
private:
int something;
};
int main(){
Child *c;
c = new Child(12);
cout << c.getSomething()<<endl;
return 0;
}
//end code
the output will not be 12, but -842150451. I suspect that I''m doing
something wrong with the virtual keyword. Can anyone explain this?
Thanks,
Larry
推荐答案
Larry Lindsey< gt ***** @ prism.gatech.edu>这样说:
Larry Lindsey <gt*****@prism.gatech.edu> spoke thus:
int main(){
Child * c;
c = new Child(12);
cout<< c.getSomething()<< ENDL;
^^^^^^^^^^^^^^^^ return 0;
}
//结束代码
int main(){
Child *c;
c = new Child(12);
cout << c.getSomething()<<endl; ^^^^^^^^^^^^^^^^ return 0;
}
//end code
你不应该做的吗
c-> getSomething()
代替? c是一个指针...
-
Christopher Benson-Manica | Jumonji giri,荣誉。
ataru(at)cyberspace.org |
Shouldn''t you have done
c->getSomething()
instead? c is a pointer...
--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
" Larry Lindsey" < GT ***** @ prism.gatech.edu>在留言中写道
新闻:bk ********** @ news-int.gatech.edu ...
"Larry Lindsey" <gt*****@prism.gatech.edu> wrote in message
news:bk**********@news-int.gatech.edu...
Aieeeeaiiieeaiiieii,啊想啊需要一些pro-zac
出于某种原因,我的对象变量不会将它们的值保持在方法范围的
之外。例如,给定:
//开始代码
class Parent {
public:
Parent(){}
virtual~Parent(){}
virtual void setSomething(int thing){}
virtual int getSomething(){return 0}
...
};
class Child:public Parent {<公开:
孩子(){...}
孩子(int thing){
...
setSomething(thing);
}
~Child(){...}
void setSomething(int thing){
something = thing;
}
int getSomething(){
返回一些东西;
}
私人:
int something;
};
int main(){
Child * c;
c =新孩子(12);
cout<< c.getSomething()<< endl;
返回0;
}
//结束代码
输出不会是12,而是-842150451。我怀疑我正在做虚拟关键字的错误。任何人都可以解释一下这个吗?
Aieeeeaiiieeaiiieii, ah think ah need some pro-zac
For some reason, my object variables aren''t keeping their values outside of the method scope. For instance, given:
//Begin code
class Parent{
public:
Parent(){}
virtual ~Parent(){}
virtual void setSomething(int thing){}
virtual int getSomething(){return 0}
...
};
class Child: public Parent{
public:
Child(){...}
Child(int thing){
...
setSomething(thing);
}
~Child(){...}
void setSomething(int thing){
something=thing;
}
int getSomething(){
return something;
}
private:
int something;
};
int main(){
Child *c;
c = new Child(12);
cout << c.getSomething()<<endl;
return 0;
}
//end code
the output will not be 12, but -842150451. I suspect that I''m doing
something wrong with the virtual keyword. Can anyone explain this?
拉里,为了您的利益,以及那些试图阅读您的代码的人,请只有
编译后的代码。上面的代码没有编译。除了
....,你不能写c.getSomething。 c是一个指针,所以语法是
c-> getSomething。
Larry, for your benefit, and for those trying to read your code, please only
post code that compiles. The code above doesn''t compile. Aside from the
...., you cannot write c.getSomething. c is a pointer, so the syntax is
c->getSomething.
" Larry Lindsey" ; < GT ***** @ prism.gatech.edu>在留言中写道
新闻:bk ********** @ news-int.gatech.edu ...
"Larry Lindsey" <gt*****@prism.gatech.edu> wrote in message
news:bk**********@news-int.gatech.edu...
Aieeeeaiiieeaiiieii,啊想啊需要一些pro-zac
出于某种原因,我的对象变量不会将它们的值保持在方法范围的
之外。例如,给定:
//开始代码
以下代码不是什么'给出了行为
你描述。它甚至不会编译。
#include< iostream>
使用std :: cout;
使用std :: endl;
class Parent {
public:
Parent(){}
virtual〜Parent(){}
virtual void setSomething(int事情){}
virtual int getSomething(){return 0}
virtual int getSomething()const {return 0;}
.. 。
// ...
};
班级孩子:公共家长{
公众:
孩子( ){...}
孩子(){/ * ... * /}
如果你写的是
孩子ch;
你得到一个处于未知状态的物体。
孩子ch2 = ch; / *未定义的行为,评估
未初始化的对象''ch''* /
更好地使用*某种*某种''默认''<当没有指定时,
值,例如:
Child():something(0){}
Child(int thing) {
...
setSomething(thing);
}
Child(int thing):something(thing){}
~Child(){...}
void setSomething(int thing){
something = thing;
}
int getSomething(){
int getSomething()const {
返回内容;
}
私有:
int something;
};
int main(){
Child * c;
c = new Child(12);
cout<< c.getSomething()<< ENDL;
此行不应编译。 ''点运算符''可以
只能用于struct,class或union类型。 ''c''
不是这些,它是指针。
cout<< c-> getSomething()<<结束;
最后,就像妈妈总是说的那样,把东西放回原点
你发现它们的方式:
删除c;
返回0;
}
//结束代码
输出不会是12,而是-842150451。
您的代码甚至无法编译。但是我怀疑,当你运行任何版本的这个编译时,你就是
访问成员对象''something''没有
已初始化。
我怀疑我正在做虚拟关键字的错误。
虚拟与此问题无关。
有人可以解释一下吗?
Aieeeeaiiieeaiiieii, ah think ah need some pro-zac
For some reason, my object variables aren''t keeping their values outside of the method scope. For instance, given:
//Begin code
The following code is not what''s giving the behavior
you describe. It won''t even compile.
#include <iostream>
using std::cout;
using std::endl;
class Parent{
public:
Parent(){}
virtual ~Parent(){}
virtual void setSomething(int thing){}
virtual int getSomething(){return 0}
virtual int getSomething() const {return 0;}
...
// ...
};
class Child: public Parent{
public:
Child(){...}
Child() { /* ... */ }
If you write e.g.
Child ch;
you get an object in an unknown state.
Child ch2 = ch; /* undefined behavior, evaluation
of uninitialized object ''ch'' */
Better to use *some* kind of ''default''
value when none is specified, e.g.:
Child() : something(0) {}
Child(int thing){
...
setSomething(thing);
}
Child(int thing) : something(thing) {}
~Child(){...}
void setSomething(int thing){
something=thing;
}
int getSomething(){
int getSomething() const {
return something;
}
private:
int something;
};
int main(){
Child *c;
c = new Child(12);
cout << c.getSomething()<<endl;
This line should not compile. The ''dot operator'' can
only be used with a struct, class, or union type. ''c''
is none of these, it''s a pointer.
cout << c->getSomething() << endl;
And finally, as Mother always said, put things back
the way you found them:
delete c;
return 0;
}
//end code
the output will not be 12, but -842150451.
Your code won''t even compile. But I suspect that when
you ran whatever version of this does compile, you''re
accessing the member object ''something'' which has not
been initialized.
I suspect that I''m doing
something wrong with the virtual keyword.
virtual has nothing to do with the problem.
Can anyone explain this?
见上文。
完整改编你的代码,给出预期结果
:
#include< iostream>
使用std :: cout;
使用std :: endl;
class Parent {
public:
Parent(){}
virtual~Parent(){}
virtual void setSomething(int thing){ } $ / $
virtual int getSomething()const {return 0;}
};
class Child:public Parent {
public:
Child(){}
Child(int thing):something(thing){}
~ Child(){}
void setSomething(int thing){
something = thing;
}
int getSomething()const {
返回一些东西;
}
私人:
int something;
};
int main(){
Child * c;
c =新孩子(12);
cout<< c-> getSomething()<< endl;
返回0;
}
输出:
12
HTH,
-Mike
See above.
A complete adaptation of your code that gives
the expected result:
#include <iostream>
using std::cout;
using std::endl;
class Parent {
public:
Parent() {}
virtual ~Parent() {}
virtual void setSomething(int thing) {}
virtual int getSomething() const {return 0;}
};
class Child: public Parent{
public:
Child() {}
Child(int thing) : something(thing) {}
~Child() {}
void setSomething(int thing){
something=thing;
}
int getSomething() const {
return something;
}
private:
int something;
};
int main(){
Child *c;
c = new Child(12);
cout << c->getSomething()<<endl;
return 0;
}
Output:
12
HTH,
-Mike
这篇关于无可变持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!