我希望在使用此代码创建对象后使用C的值..如何继续 [英] I Want To Use The Value Of C After Object Is Created With This Code.. How Can I Proceed
本文介绍了我希望在使用此代码创建对象后使用C的值..如何继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include<iostream>
#include<conio.h>
using namespace std;
class base
{
public:
base()
{
int a =10;
int b =20;
int c = a+b;
}
};
int main()
{
base a;
getch();
return 0;
}
推荐答案
你不能使用(变量)c的值
:它是一个局部变量(它的生命周期在Base
构造函数中,即构造函数执行后,c
不再存在)。尝试:
You cannot use the value of the (variable)c
: it is a local variable (its lifetime is scoped inBase
constructor, that is, after constructor execution,c
doesn't exist anymore). Try:
class base
{
public:
int c;
base()
{
int a =10;
int b = 20;
c = a + b;
}
};
int main()
{
base a;
cout << "c = " << a.c << endl;
}
这篇关于我希望在使用此代码创建对象后使用C的值..如何继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文