class array constructor help

This is a discussion on class array constructor help within the C++ Programming forums, part of the General Programming Boards category; How can i call non default constructor for array of object e,g myClass array[8]???//what to wrte here...

  1. #1
    Registered User cfrost's Avatar
    Join Date
    Apr 2004
    Posts
    119

    Angry class array constructor help

    How can i call non default constructor for array of object
    e,g
    myClass array[8]???//what to wrte here

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you can try something like this:

    Code:
    class myClass
    {
    int num;
    public:
    myClass(int num_) : num(num_) {};
    };
     
    int num_ = 0;
    myClass * array = new myClass[8](num_);

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    You can try, but.....
    >> error C2538: new : cannot specify initializer for arrays

    You'll have allocate individual elements of the array dynamically if you want to use a non-default constructor.

    gg

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >You'll have allocate individual elements of the array dynamically if you want to use a non-default constructor.
    Or initialize an array manually:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class test {
      int i;
    public:
      test ( int init )
        : i ( init )
      {}
      void print()
      {
        cout<< i <<endl;
      }
    };
    
    int main()
    {
      test a[5] = {
        test ( 0 ),
        test ( 1 ),
        test ( 2 ),
        test ( 3 ),
        test ( 4 )
      };
    
      for ( int i = 0; i < 5; i++ )
        a[i].print();
    }
    My best code is written with the delete key.

  5. #5
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Code:
    class Test {
    public:
       test();
    private:
       int t[5];
    };
    
    Test::test(){
       t[ 2 ] = 2;
    }

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Quote Originally Posted by cyberCLoWn
    Code:
    class Test {
    public:
       test();
    private:
       int t[5];
    };
    
    Test::test(){
       t[ 2 ] = 2;
    }
    And that has any relevance to the question, how?
    My best code is written with the delete key.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    It doesn't. Oh wait... damn... those rhetorical questions always get me.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. Animation class not working
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 03-02-2005, 05:48 AM
  3. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM

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