Class member array size with constuctor initalizer

This is a discussion on Class member array size with constuctor initalizer within the C++ Programming forums, part of the General Programming Boards category; I remember reading somewhere that it was possible to have a class with an array whose size was determined at ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    46

    Class member array size with constuctor initalizer

    I remember reading somewhere that it was possible to have a class with an array whose size was determined at instantiation time. So that the size of the internal array size would be constant for the life of the object but that the array size was gotten from an input parameter and used in a constructor initalizer. Something like the very broken...
    Code:
    #include <iostream>
    using namespace std;
    
    class oo{
    public:
        int ii[];
        oo(int n): ii(n){}; //Trying to setup array size here
    };
    
    int main(int argc, char* argv[]){
        oo xx(10);
    
        for (int i = 0; i < 10; ++i){
            xx.ii[i] = i;
            cout << xx.ii[i] << endl;
        }
    }
    Dispite the errors above, is what I'm trying to accomplish possible?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You cannot do it like that. Instead you need a pointer and then you can dynamically allocate memory at run-time. Try this out.

    Code:
    #include <iostream>
    using namespace std;
    
    class oo
    {
      public:
        int *ii;
        oo(int n);
        ~oo( );
    };
    
    oo::oo( int n )
    {
      ii = new int[ n ];
    }
    
    oo::~oo( )
    {
      delete [] ii;
    }
    
    int main(int argc, char* argv[]){
        oo xx(10);
    
        for (int i = 0; i < 10; ++i){
            xx.ii[i] = i;
            cout << xx.ii[i] << endl;
        }
    
        return 0;
    }
    Hope this helps you.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    46
    Thanks again MrWizard... I was afraid you were going to say that. I did a little reading over the weekend & I think I understand the initializer thing. These are just calls to the subclass/field's construtor. So to even get close to what I wanted I would have to call a class that had a table like constructor...
    Code:
    class table{
      int* ii;
      table(int n){ ii = new int[n]; }
      ~table(void){delete [] ii;}
    };
    
    class oo{
       table tab;
       oo(int n) : tab(n) {};
    };
    but this is silly & just hides the new/delete & adds overhead. all I wanted to do is a stack pointer (or heap) bump by n * obj_size directly & call the initializer n times. Not possible *sigh*

    thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM
  5. grow function to change size of array class
    By Santos7772002 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 05:31 AM

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