Couldnt be initialized~

This is a discussion on Couldnt be initialized~ within the C++ Programming forums, part of the General Programming Boards category; I found I couldnt initialize the variables in class, is it a rule ? it is so bad if so~...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Exclamation Couldnt be initialized~

    I found I couldnt initialize the variables in class, is it a rule ?

    it is so bad if so~
    Never end on learning~

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: Couldnt be initialized~

    Originally posted by black
    I found I couldnt initialize the variables in class, is it a rule ?

    it is so bad if so~
    i've never written one line of c++ in my life, but couldnt you initialize them in the constructor?
    hello, internet!

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Re: Re: Couldnt be initialized~

    Originally posted by moi
    i've never written one line of c++ in my life, but couldnt you initialize them in the constructor?
    huh ? a yes, I almost forgot we have constructor at all~ thanx~

    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~

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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

  5. #5
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    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;
    thanx but what is that if(p) ?
    Never end on learning~

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21