constructor's parameters

Hello...

I created an object from a class... Inside this object I want to create an array of objects of another class, but with different parameters... What is the syntax to pass parameters to a constructor??

Thanks...
Just like a normal function:
1
2
3
4
5
6
7
8
9
10
class Foo
{
public:
Foo(int, float, char, whatEverElseYouNeed);
}

Foo::Foo(...)
{
//Code
}
First class header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef ONE_H
#define ONE_H


class One
{
    public:
        One(int a, int b);
    private:
        int x;
        int y;
};

#endif // ONE_H 


First class cpp file:

1
2
3
4
5
6
#include "One.h"

One::One(int a, int b)
:   x(a), y(b)
{
}


Second class header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef TWO_H
#define TWO_H
#include "One.h"

class Two
{
    public:
        Two();
    private:
        One one_object[2];
};

#endif // TWO_H 


Second class cpp file:

1
2
3
4
5
6
7
8
#include "Two.h"
#include "One.h"

Two::Two()
:   one_object[0](1,1),
    one_object[1](2,2)
{
}


The second class cpp file has an error...
Please help...
You can't use the initialization list to initialize arrays. You'll have to stick it in the constructor's body.
You can't call a non-default constructor for arrays at all. The objects are all default-constructed when the array is created.
1
2
3
4
5
6
7
8
9
10
11
12
struct A
{
    A( int ii, double dd )  : i(ii), d(dd) {}
    int i ;
    double d ;
};

struct B
{
    B() : array{ { 1, 2.3 }, { 4, 5.6 }, { 7, 8.9 }, { 0, 0 } } {}
    A array[4] ;
};

http://www2.research.att.com/~bs/C++0xFAQ.html#uniform-init
Topic archived. No new replies allowed.