Thread: Passing array through class constructor

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    2

    Passing array through class constructor

    I am having trouble creating a class and initializing the array class variables.

    Code:
    class Foo
    {
    	public:
            int arrayOfNum[];
            
    		Foo();
    		Foo(int arrayIN[]);
    		~Foo();
    };
    
    Foo::Foo(int arrayIN[])
    {
    	arrayOfNum = arrayIN; //This is where I'm having problems at.
    }
    I am still new to C++ (learned programming in Java) and is still grasping the concept of arrays and pointers. Could someone show me how passing an array through a constructor to initialize an object should look like?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    For C style arrays like the one you have, you need to set a size. Your array has no size in the class declaration: int arrayOfNum[];

    Once you set a size, your constructor would normally use a for loop to copy each item one at a time (you could also use the std::copy algorithm).

    However, in C++, you should really be using a vector instead of the C style array. You can assign vectors with = like you tried to do with the array above. You also don't have to set the size immediately, you can wait until you know how many elements you want to add.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    C++ only knows the length of a char array by itself, so your class constructor would need a pointer to the array instead, and a second parameter for the size of the array. Your constructor will need a lot of changes to work properly.

    First, your array must be allocated by new:
    Code:
    int foo_array = new int[foo_size];
    Then you must loop through the array you want stored, using the pointer passed in to fill the class' array.
    Code:
     for(int i(0); i < foo_size; i++) {
       foo_array[i] = array_in[i];
    }
    Your destructor must call delete on the foo_array:
    Code:
    delete[] foo_array;
    Your problem is also best solved, I think, by simply containing a vector as a member. Since I am assuming you don't know what vectors are, you will need to learn. The sooner you do that, the better.
    Last edited by whiteflags; 05-10-2006 at 01:40 PM.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    2
    Much appreciated, I understand now why I can't use the array as such. Cheers

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> your array must be allocated by new.
    You can have a fixed size array in the class, you just have to specify the size at compile-time. If you know the size at compile time, you don't need the size for the constructor's parameter, nor do you need a second parameter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-26-2006, 12:59 AM
  2. passing constructor data to other constructors
    By eth0 in forum C++ Programming
    Replies: 9
    Last Post: 05-21-2004, 03:20 AM
  3. class array constructor help
    By cfrost in forum C++ Programming
    Replies: 6
    Last Post: 04-15-2004, 02:08 PM
  4. passing array to a constructor
    By frenchfry164 in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2003, 04:22 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM