avatar
Test your C++ knowledge...# Programming - 葵花宝典
a*n
1
I am reading a book, notice this.
Do you know why including the commented lines (5th or/and 8th lines) will throw
compile error? Please answer it without trying on the machine.
/////////////////////////////////////////////////////////
class X {
private:
double m;
static double n;
//X member1; // Will give compile error, do you know why?
static X member2;
public:
//X(double d = m); // will give compile error, do you know why?
X(int, double d = n
avatar
c*e
2
because of static keyword.
1. u can only have pointer or reference or any conbination of the two for ty
pe X as non static member.
2. u can not use non static member as ur constructor augments
what book is this? i would recommend u to read another book

【在 a**n 的大作中提到】
: I am reading a book, notice this.
: Do you know why including the commented lines (5th or/and 8th lines) will throw
: compile error? Please answer it without trying on the machine.
: /////////////////////////////////////////////////////////
: class X {
: private:
: double m;
: static double n;
: //X member1; // Will give compile error, do you know why?
: static X member2;

avatar
a*n
3
Andersons' book. Maybe it is not well-known. It is pretty good comparing to
other C++ books. Which is your book?
You listed some rules, but it is not very clear why there are these rules.
(I think I know the answers)
Rules are easy to be forgotten.

【在 c********e 的大作中提到】
: because of static keyword.
: 1. u can only have pointer or reference or any conbination of the two for ty
: pe X as non static member.
: 2. u can not use non static member as ur constructor augments
: what book is this? i would recommend u to read another book

avatar
c*e
4
I didnt read any book nor did i looked up the C++ standard for these rules.
Its all logical conclusion.
1. If u have struct X { X x;} then there is a loop of composition in there.
compiler will not allow this to happen.
2. before the constructor is called the object is not fully constructed, in
ur case, as the augment input, the non-static member is definitely no
t initialized...and u want to use it?

【在 a**n 的大作中提到】
: Andersons' book. Maybe it is not well-known. It is pretty good comparing to
: other C++ books. Which is your book?
: You listed some rules, but it is not very clear why there are these rules.
: (I think I know the answers)
: Rules are easy to be forgotten.

avatar
p*f
5

wait a minute, I think the default parameter should be a compile-time
constant, is it not?

【在 c********e 的大作中提到】
: I didnt read any book nor did i looked up the C++ standard for these rules.
: Its all logical conclusion.
: 1. If u have struct X { X x;} then there is a loop of composition in there.
: compiler will not allow this to happen.
: 2. before the constructor is called the object is not fully constructed, in
: ur case, as the augment input, the non-static member is definitely no
: t initialized...and u want to use it?

avatar
c*e
6
it is, then?

【在 p******f 的大作中提到】
:
: wait a minute, I think the default parameter should be a compile-time
: constant, is it not?

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。