Wikipedia says something like this

Aggregate classes
An aggregate class is a class with no user-declared constructors, no private or protected non-static data members, no base classes, and no virtual functions[1]. Such a class can be initialized with a brace-enclosed comma-separated list of initializer-clauses. The following code has the same semantics in both C and C++:

struct C
{
  int a;
  double b;
};
 
struct D
{
  int a; 
  double b;
  C c;
};
 
// initialize an object of type C with an initializer-list
C c = { 1, 2 };
 
// D has a sub-aggregate of type C. In such cases initializer-clauses can be nested
D d = { 10, 20, { 1, 2 } };

if i have something like this :
struct C c[4];
how will i fill the items of that array?
c[0] = ?????

Thanks inadvance

Recommended Answers

All 6 Replies

>An aggregate class is [...]
Also known as a POD (plain old data) type.

>how will i fill the items of that array?
If you want to initialize it with a list like with the single C object, it's just the nested equivalent (really no different in principle from how you initialized d):

struct C c[4] = {
  {1, 2},
  {3, 4},
  {5, 6},
  {7, 8}
};

After the array is initialized (whether you use an initializer or not), you can't use an initializer later on because they're only allowed in definitions, so this is illegal:

C c[4];

c[0] = {1, 2};

In such a case you would need to manually fill out the data members:

C c[4];

c[0].a = 1;
c[0].b = 2;
commented: without her it would be so hard +3

Thank you so much Julienne, you saved me lots of time.

I think i have a problem with it, to make a demonstration i created two files as test.h and test.cpp.

test.h :

class test
{
  public:
    test();
  struct deneme
  {
    int a;
    int b;
  };
 struct deneme denemeler[5];
};

test.cpp :

#include "test.h"
#include <iostream>
using namespace std;
test::test()
{
 struct deneme denemeler[5] = {{1,2},{3,4},{5,6},{7,8},{9,10}};
}

int main()
{
  test myTest;
  for(int i=0;i<5;i++)
  {
    cout << myTest.denemeler[i].a << endl;
  }
  return 0;
}

when i run the code the output is as follows :

-559038737
-559038737
1
-559038737
536874698

why do i see this?

if i do it in the same file without declaring it as a class member it works perfectly :

test2.cpp :

#include <iostream>
using namespace std;

struct deneme
{
  int a;
  int b;
};

struct deneme denemeler[5]= {{1,2},{3,4},{5,6},{7,8},{9,10}};


int main()
{
for(int i=0;i<5;i++)
  {
    cout << denemeler[i].a << endl;
  }
  return 0;

  
}

output is as follows :

1
3
5
7
9

but i have to declare that member in my class header file then fill it in my class definition file, how will i do that?

test::test()
{
 struct deneme denemeler[5] = {{1,2},{3,4},{5,6},{7,8},{9,10}};
}

I think that the above function is defining a new denemeler of deneme type and then as the constructor ends it gets destroyed.

Hence you loose those values. After that your actual denemeler member doesnt contain any value. So hence it returns out other numbers.


I guess you should define an "=" operator for your struct and perform a copy in the constructor

test::test()
{
 struct deneme denemeler2[5] = {{1,2},{3,4},{5,6},{7,8},{9,10}};
int x=0;
while(x<5)
{
denemeler[x]=denemeler2[x];
}
}

>why do i see this?
You've indirectly discovered a limitation of using arrays as class members. You can't initialize them with arbitrary data. The only way to "initialize" an array member is through assignment as Sky Diploma suggested.

The closest you can get is default initialization:

test::test(): denemeler() {}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.