C++中类成员变量能否在“声明”时给初值?
形如classA{inta=0;};可行否?给出依据。1#include<iostream>2usingnamespacestd;34classA5{6inta=0;7}...
形如
class A
{
int a=0;
};
可行否?
给出依据。
1 #include <iostream>
2 using namespace std;
3
4 class A
5 {
6 int a=0;
7 };
8
9 int main()
10 {
11 A test;
12 return 0;
13 }
************
g++ test.init.cpp
test.init.cpp:6: error: ISO C++ forbids initialization of member ‘a’
test.init.cpp:6: error: making ‘a’ static
test.init.cpp:6: error: ISO C++ forbids in-class initialization of non-const static member ‘a’
************
知道答案了 展开
class A
{
int a=0;
};
可行否?
给出依据。
1 #include <iostream>
2 using namespace std;
3
4 class A
5 {
6 int a=0;
7 };
8
9 int main()
10 {
11 A test;
12 return 0;
13 }
************
g++ test.init.cpp
test.init.cpp:6: error: ISO C++ forbids initialization of member ‘a’
test.init.cpp:6: error: making ‘a’ static
test.init.cpp:6: error: ISO C++ forbids in-class initialization of non-const static member ‘a’
************
知道答案了 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询