作者freesamael (烧卖)
站内Programming
标题Re: [问题] c++ class中带有参数的成员初始化
时间Sun Dec 16 18:08:46 2007
※ 引述《readonly (唯读)》之铭言:
: class A
: {
: public:
: A( int c ){};
: ~A(){};
: };
: class B
: {
: public:
: B(){};
: ~B(){};
: A a( 5 ); // 这样不行吗?
: };
: 会出现下列的错误讯息:
: error: expected identifier before numeric constant
: error: expected `,' or `...' before numeric constant
: error: ISO C++ forbids declaration of `parameter' with no type
: 看来是把它当成一个function definition了,
: 不知道正确的写法是怎样呢?
修正眼睛沾到狗屎的错误...
class B {
public:
B() : a(5) {}
~B() {}
A a; <-- 不过这东西还是放 private/protected 比较好
};
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.135.82.153
1F:推 avhacker:人家又没说 B 要继承 A -_- 140.123.19.235 12/16 19:36
2F:→ WPC001:看起来应该是A a; 然後在B() : A(5) {} 123.195.25.91 12/16 19:42
3F:推 avhacker:楼上也错. 应该是 B() : a(5){} 140.123.19.235 12/16 20:53
4F:→ freesamael:咦? 喔...眼残看错 220.135.82.153 12/16 21:16
※ 编辑: freesamael 来自: 220.135.82.153 (12/16 21:18)