a**n 发帖数: 313 | 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 | c********e 发帖数: 383 | 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;
| a**n 发帖数: 313 | 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
| c********e 发帖数: 383 | 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.
| p******f 发帖数: 162 | 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?
| c********e 发帖数: 383 | 6 it is, then?
【在 p******f 的大作中提到】 : : wait a minute, I think the default parameter should be a compile-time : constant, is it not?
|
|