Thread: initialize member array in initializer list?

  1. #1
    Registered User Vick jr's Avatar
    Join Date
    May 2009
    Location
    Galifray
    Posts
    27

    initialize member array in initializer list?

    I have a class (ClassA) with a member array of instances of another class (Classb).
    In the constructor, I want to send in a pointer to an existing array, as well as the size of the array.

    Is there a way to initialize the member array in the initializes list along with all the other member variables?

    If not, how would I initialize it in the constructor body?

    Right now ClassB has no default constructor. Does it have to?
    Code:
    class ClassA{
    	private:
    		ClassB myarray[];
    }
    
    //this constructor doesn't work
    Classa::ClassA(ClassB*, int n):
    	myarray(B)// "cannot specify explicit initializer for arrays"
    {
    }
    
    //this constructor doesn't work either
    ClassA::ClassA(ClassB*, int n):
    	myarray(new ClassB[n])//"no appropriate default constructor available"
    {
    }
    I have found one way which compiles:

    Code:
    class ClassA{
    	private:
    		ClassB* myarray;
    }
    
    Classa::ClassA(ClassB*, int n):
    	myarray(B)
    {
    }
    but will this work the way I want it to?

    what's the best way to do this?

    Thanks!

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    30
    Quote Originally Posted by Vick jr View Post
    I have a class (ClassA) with a member array of instances of another class (Classb).
    In the constructor, I want to send in a pointer to an existing array, as well as the size of the array.

    Is there a way to initialize the member array in the initializes list along with all the other member variables?

    If not, how would I initialize it in the constructor body?

    Right now ClassB has no default constructor. Does it have to?
    Code:
    class ClassA{
    	private:
    		ClassB myarray[];
    }
    
    //this constructor doesn't work
    Classa::ClassA(ClassB*, int n):
    	myarray(B)// "cannot specify explicit initializer for arrays"
    {
    }
    
    //this constructor doesn't work either
    ClassA::ClassA(ClassB*, int n):
    	myarray(new ClassB[n])//"no appropriate default constructor available"
    {
    }
    I have found one way which compiles:

    Code:
    class ClassA{
    	private:
    		ClassB* myarray;
    }
    
    Classa::ClassA(ClassB*, int n):
    	myarray(B)
    {
    }
    but will this work the way I want it to?

    what's the best way to do this?

    Thanks!

    Yes, you cannot create an array of objects for which you don't have the default constructor.
    I am curious about how you create the array of classB objects in the first place without having the default constructor before passing it to the classA constructor.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    11
    The problem is that you cannot declare an array as a class member the way you have int the first version of your class declaration, since the compiler doesn't know how much memory to allocate on the stack. Your second version does it the correct way.

    Since myarray is really just a pointer to the first element in the array, when you use the initialization list as you have, you are only copying the pointer. If you want to copy each element, you can do as you did the first time in the initialization list to allocate the memory, but yes, you also must copy each element in the body of the constructor manually.

    Code:
    ClassA::ClassA(ClassB *otherArray, int n) :
    myarray(new ClassB[n])
    {
        for (int i=0; i<n; i++)
        {
            myarray[i] = otherArray[i];
        }
    }
    (I have left out error checking for the sake of brevity)
    Last edited by jrohde; 05-19-2010 at 07:53 PM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    "cannot specify explicit initializer for arrays" means what it says.
    It is not possible to initialise an array inside an initialisation list in C++. You must do it inside the body of the constructor.

    Initialisating a pointer as per your second example isn't a useful direct substitute by any measure, as it's doing something totally different, with different responsibilities for other parts of your program.

    jrohde has posted something that will work.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User Vick jr's Avatar
    Join Date
    May 2009
    Location
    Galifray
    Posts
    27
    Quote Originally Posted by iMalc View Post
    "cannot specify explicit initializer for arrays" means what it says.
    It is not possible to initialise an array inside an initialisation list in C++. You must do it inside the body of the constructor.

    Initialisating a pointer as per your second example isn't a useful direct substitute by any measure, as it's doing something totally different, with different responsibilities for other parts of your program.

    jrohde has posted something that will work.
    By which you mean that whatever created the original array must keep it in memory until ClassA is done using the pointer to it? Hemm...

    I was eventually planning on having another overloaded constructor create the array dynamically be reading from a file. Perhaps I should implement that now. I was just hoping to get an easier version working first.

    Again, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem defining structure
    By MTK in forum C Programming
    Replies: 12
    Last Post: 09-08-2009, 03:26 PM
  2. circularly linked list
    By BlackOps in forum C Programming
    Replies: 0
    Last Post: 07-18-2009, 08:12 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 10:07 PM

Tags for this Thread