How to Initialize an Array in C++ Objects

c++03 Initializing a array of objects with multiple parameters

You can use std::generate

Example:

A generator(){ return A(1,2); }

std::generate( a, a + (sizeof(a) / sizeof(a[0])), generator );

How to initialize an array in C++ objects

You need to initialize the array in the constructor initialization list

#include <iostream>

class Something {
private:

int myArray[10];

public:

Something()
: myArray { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }
{
}

int ShowThingy(int what) {
return myArray[what];
}

~Something() {}
};

int main () {
Something Thing;
std::cerr << Thing.ShowThingy(3);
}

..\src\Something.cpp:6:51: sorry, unimplemented: non-static data member initializers

C++11 also adds supports for inline initialization of non-static member variables, but as the above error message states, your compiler has not implemented this yet.

How do I initialize an empty array of objects in C++?

Don't use arrays, use:

 std::vector<Contact> addressBook;

instead of

 static Contact addressBook[CAPACITY];

And do you really need to define it static?

With vector you don't need the variable "used". If you want to know how many contacts you have, you only need to write

 addressBook.size();

Now, if you want to look for a specific contact, you can use find:

 if(find(addressBook.begin(), addressBook.end(), my_contact) != addressBook.end()){
...
}

Initialize array of objects in constructor

Since MyClass(SomeCStruct* pCStruct) constructor doesn't (can't) initialize the TheOtherClass m_objects in the member initializer list, the m_objects will need to be default constructed at first, and then this member will be reassigned with new value in the body of the MyClass constructor.

The TheOtherClass won't have a synthesized constructor since you have defined (other, non-default) ctor that takes SomeCStruct* and int. Thus, no default constructor for it in your code.

Here, we define a default constructor:

class TheOtherClass {
SomeCStruct* m_pCStruct;
int m_ObjIdx;
public:
TheOtherClass() : m_pCStruct(nullptr), m_ObjIdx(0) {} // Default ctor
TheOtherClass(SomeCStruct* pCStruct, int ClassIdx)
: m_pCStruct(pCStruct),
m_ObjIdx(ClassIdx) {}
};

As to the MyClass. Since you've already pass the argument as a pointer MyClass(SomeCStruct* pCStruct), you don't need to take the address of it (as in &pCStruct).

And the main issue in the MyClass is that the m_objects is of class type TheOtherClass which does not define any operator=, including the one that takes brace-enclosed initializer list. So, you won't be able to do like so:

m_objects[4] = { // The operator '=' has not been defined for the TheOtherClass, also note the subscript
TheOtherClass(pCStruct, 1),
TheOtherClass(pCStruct, 2),
TheOtherClass(pCStruct,3),
TheOtherClass(pCStruct, 4)
};

Again, when you try to list initialize it, it has already been default constructed. Thus, for this approach to work you will need to define such copy-assignment operator to do this kind of assignment to an already default constructed instance of the TheOtherClass. As to how to implement this operator, it will mostly depend on the overall design and other factors which were not mentioned in this thread.

Update

If you will have the following body of the MyClass constructor, it will work:

MyClass(SomeCStruct* pCStruct) : m_pMyCStruct(pCStruct) {
// Assign to default initialized TheOtherClass object
m_objects[0] = TheOtherClass(pCStruct, 1);
}

I would advise you to check these:

Operator overloading

Constructors and member initializer lists

How to initialize all members of an array to the same value?

Unless that value is 0 (in which case you can omit some part of the initializer
and the corresponding elements will be initialized to 0), there's no easy way.

Don't overlook the obvious solution, though:

int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

Elements with missing values will be initialized to 0:

int myArray[10] = { 1, 2 }; // initialize to 1,2,0,0,0...

So this will initialize all elements to 0:

int myArray[10] = { 0 }; // all elements 0

In C++, an empty initialization list will also initialize every element to 0.
This is not allowed with C until C23:

int myArray[10] = {}; // all elements 0 in C++ and C23

Remember that objects with static storage duration will initialize to 0 if no
initializer is specified:

static int myArray[10]; // all elements 0

And that "0" doesn't necessarily mean "all-bits-zero", so using the above is
better and more portable than memset(). (Floating point values will be
initialized to +0, pointers to null value, etc.)

Initialized a pointer array in C - Variable sized object may not be initialized

Variable length arrays may not be initialized in their declarations.

You can use the standard string function memset to initialize the memory occupied by a variable length array.

For example

#include <string.h>

//...

int c = 15;
Struct *Pointer[c];

memset( Pointer, 0, c * sizeof( *Pointer ) );

Pay attention to that variable length arrays shall have automatic storage duration that is they may be declared in a function and may not have the storage specifier static.



Related Topics



Leave a reply



Submit