由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Test your C++ knowledge...
相关主题
static initialization dependency c++问一个java基础的初始化的问题,一直搞不明白
c++ initialize struct抠字眼:assignment and initialize in C++
问一个 copy constructor 的问题 (C++)请问static variable init的问题?
question about c++ constructorC++默认的copy constructor的疑惑
一道 memset in C++的题static vector 怎么 initialize ?
c++ 不自动initialize变量么?关于C/C++里的Static variable的memory allocation/initializa
c++ 一问pthread_create inside a constructor
what is the difference?C++: Static initialization dependency
相关话题的讨论汇总
话题: compile话题: static话题: double话题: c++话题: do
进入Programming版参与讨论
1 (共1页)
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?

1 (共1页)
进入Programming版参与讨论
相关主题
C++: Static initialization dependency一道 memset in C++的题
问个copy constructor的问题c++ 不自动initialize变量么?
关于数组动态分配的疑问???c++ 一问
c++:constructor 一问what is the difference?
static initialization dependency c++问一个java基础的初始化的问题,一直搞不明白
c++ initialize struct抠字眼:assignment and initialize in C++
问一个 copy constructor 的问题 (C++)请问static variable init的问题?
question about c++ constructorC++默认的copy constructor的疑惑
相关话题的讨论汇总
话题: compile话题: static话题: double话题: c++话题: do