I found I couldnt initialize the variables in class, is it a rule ?
it is so bad if so~
I found I couldnt initialize the variables in class, is it a rule ?
it is so bad if so~
Never end on learning~
i've never written one line of c++ in my life, but couldnt you initialize them in the constructor?Originally posted by black
I found I couldnt initialize the variables in class, is it a rule ?
it is so bad if so~
hello, internet!
huh ? a yes, I almost forgot we have constructor at all~ thanx~Originally posted by moi
i've never written one line of c++ in my life, but couldnt you initialize them in the constructor?
and anyone knows how to initialize this array ?
int *p;
p=new int[5];
p[0]=5;
I tried p=new int[5]{1,2,3,4,5} but failed~
Never end on learning~
Code:int* p= new int[5]; if(p) { for(int i=0;i<5;i++) *p=i; } // do **** with p[] then delete [] p;
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
thanx but what is that if(p) ?Originally posted by Stoned_Coder
Code:int* p= new int[5]; if(p) { for(int i=0;i<5;i++) *p=i; } // do **** with p[] then delete [] p;
Never end on learning~
The standard says that if new fails then an exception of type std::bad_alloc is thrown unless the nothrow version of new is used.If your compiler supports exceptions properly and throws when new fails then this test is meaningless. On some current compilers though the default behaviour of new is to return 0 if memory could not be allocated.So the if statement just checks that new succeeded.I probably should have left it out as new failing would only happen in cases of memory exhaustion but it just shows that you shouldnt take anything for granted.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi