Connecting Tech Pros Worldwide Forums | Help | Site Map

How to initialize array in class

www.brook@gmail.com
Guest
 
Posts: n/a
#1: Mar 3 '06
hi, I have a class

class A
{
const int m_a;
const int m_b[2];
}

m_a can be initialized at the constructor
A():m_a(2)
{
}

But how do I do the same thing to m_b? I know one of the possible
solutions is to use vector<int> for b instead of using direct array.
But I am just curious

Thanks


Alf P. Steinbach
Guest
 
Posts: n/a
#2: Mar 3 '06

re: How to initialize array in class


* www.brook@gmail.com:[color=blue]
> hi, I have a class
>
> class A
> {
> const int m_a;
> const int m_b[2];
> }
>
> m_a can be initialized at the constructor
> A():m_a(2)
> {
> }
>
> But how do I do the same thing to m_b? I know one of the possible
> solutions is to use vector<int> for b instead of using direct array.
> But I am just curious[/color]

You can default-initialize it (perhaps with C++2003 that is
value-initialize, but it's the same thing for this problem) with

A():m_b() {}

Otherwise you'll have to call a function that does the initialization,
or do it directly in the constructor body.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
shellux
Guest
 
Posts: n/a
#3: Mar 3 '06

re: How to initialize array in class


>You can default-initialize it (perhaps with C++2003 that is[color=blue]
>value-initialize, but it's the same thing for this problem) with[/color]
[color=blue]
> A():m_b() {}[/color]

[color=blue]
>Otherwise you'll have to call a function that does the initialization,
>or do it directly in the constructor body.[/color]


I have test a similar class as follow(in VC6.0) , but, as the
consequence, linker offer an error.
class Te {
public:
Te() : m_int() {
}

private:
const int m_int[10];
};

Linker:d:\program_production\c++\visual_c++\practi ce\commutytry\communitytry.cpp(3)
: error C2439: 'm_int' : member could not be initialized

d:\program_production\c++\visual_c++\practice\comm utytry\communitytry.cpp(7)
: see declaration of 'm_int'

Alf P. Steinbach
Guest
 
Posts: n/a
#4: Mar 3 '06

re: How to initialize array in class


* shellux:[color=blue][color=green]
>> You can default-initialize it (perhaps with C++2003 that is
>> value-initialize, but it's the same thing for this problem) with[/color]
>[color=green]
> > A():m_b() {}[/color]
>
>[color=green]
>> Otherwise you'll have to call a function that does the initialization,
>> or do it directly in the constructor body.[/color]
>
>
> I have test a similar class as follow(in VC6.0) , but, as the
> consequence, linker offer an error.
> class Te {
> public:
> Te() : m_int() {
> }
>
> private:
> const int m_int[10];
> };
>
> Linker:d:\program_production\c++\visual_c++\practi ce\commutytry\communitytry.cpp(3)
> : error C2439: 'm_int' : member could not be initialized
>
> d:\program_production\c++\visual_c++\practice\comm utytry\communitytry.cpp(7)
> : see declaration of 'm_int'[/color]

Please be accurate in your reporting: you don't have a linker error but
a compilation error, and it looks to me as if you're using version 8.0,
not version 6.0.

That said, Visual C++ 6.0 is not the most standard-conforming compiler
in the world. Version 7.1 is much better (and 8.0 better still).
However, I tried your code with version 7.1, and the compiler failed;
for non-const it didn't issue an erronous diagnostic, but generated
incorrect non-initializing machine code.

It seems that g++ handles this correctly, and of course, Comeau does.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Tomás
Guest
 
Posts: n/a
#5: Mar 3 '06

re: How to initialize array in class


www.brook@gmail.com posted:
[color=blue]
> hi, I have a class
>
> class A
> {
> const int m_a;
> const int m_b[2];
> }
>
> m_a can be initialized at the constructor
> A():m_a(2)
> {
> }
>
> But how do I do the same thing to m_b? I know one of the possible
> solutions is to use vector<int> for b instead of using direct array.
> But I am just curious
>
> Thanks[/color]
The following doesn't compile, but maybe you could go for something along
these lines:

template <class T, std::size_t i>
class DefaultInitialised
{
public:

T object[i];

DefaultInitialised() : object() {}

operator (&T)[i]()
{
return object;
}
};

class A
{
private:

const DefaultInitialised<int> ka;
DefaultInitialised<int, 2> kb;

public:

int const& a;
int (&b)[2];

A() : a(ka.object), b(kb.object) {}

};

int main()
{
A k;

k.b[2] = k.a;
}

-Tomás
Tomás
Guest
 
Posts: n/a
#6: Mar 3 '06

re: How to initialize array in class


www.brook@gmail.com posted:
[color=blue]
> hi, I have a class
>
> class A
> {
> const int m_a;
> const int m_b[2];
> }
>
> m_a can be initialized at the constructor
> A():m_a(2)
> {
> }
>
> But how do I do the same thing to m_b? I know one of the possible
> solutions is to use vector<int> for b instead of using direct array.
> But I am just curious
>
> Thanks[/color]
The following doesn't compile, but maybe you could go for something along
these lines:

template <class T, std::size_t i>
class DefaultInitialised
{
public:

T object[i];

DefaultInitialised() : object() {}

operator (&T)[i]()
{
return object;
}
};

class A
{
private:

const DefaultInitialised<int> ka;
DefaultInitialised<int, 2> kb;

public:

int const& a;
int (&b)[2];

A() : a(ka.object), b(kb.object) {}

};

int main()
{
A k;

k.b[2] = k.a;
}

-Tomás
Tomás
Guest
 
Posts: n/a
#7: Mar 3 '06

re: How to initialize array in class


www.brook@gmail.com posted:
[color=blue]
> hi, I have a class
>
> class A
> {
> const int m_a;
> const int m_b[2];
> }
>
> m_a can be initialized at the constructor
> A():m_a(2)
> {
> }
>
> But how do I do the same thing to m_b? I know one of the possible
> solutions is to use vector<int> for b instead of using direct array.
> But I am just curious
>
> Thanks[/color]
The following doesn't compile, but maybe you could go for something along
these lines:

template <class T, std::size_t i>
class DefaultInitialised
{
public:

T object[i];

DefaultInitialised() : object() {}

operator (&T)[i]()
{
return object;
}
};

class A
{
private:

const DefaultInitialised<int> ka;
DefaultInitialised<int, 2> kb;

public:

int const& a;
int (&b)[2];

A() : a(ka.object), b(kb.object) {}

};

int main()
{
A k;

k.b[2] = k.a;
}

-Tomás
www.brook@gmail.com
Guest
 
Posts: n/a
#8: Mar 3 '06

re: How to initialize array in class


Since m_b is a const in array, that means there is no appropriate
constructor using VC6?

mlimber
Guest
 
Posts: n/a
#9: Mar 3 '06

re: How to initialize array in class


shellux wrote:[color=blue][color=green]
> >You can default-initialize it (perhaps with C++2003 that is
> >value-initialize, but it's the same thing for this problem) with[/color]
>[color=green]
> > A():m_b() {}[/color]
>
>[color=green]
> >Otherwise you'll have to call a function that does the initialization,
> >or do it directly in the constructor body.[/color]
>
>
> I have test a similar class as follow(in VC6.0) , but, as the
> consequence, linker offer an error.
> class Te {
> public:
> Te() : m_int() {
> }
>
> private:
> const int m_int[10];
> };
>
> Linker:d:\program_production\c++\visual_c++\practi ce\commutytry\communitytry.cpp(3)
> : error C2439: 'm_int' : member could not be initialized
>
> d:\program_production\c++\visual_c++\practice\comm utytry\communitytry.cpp(7)
> : see declaration of 'm_int'[/color]

As the OP noted, you could do something like this if you use
std::vector:

http://groups.google.com/group/comp....75a9bd905b06c0

Cheers! --M

www.brook@gmail.com
Guest
 
Posts: n/a
#10: Mar 5 '06

re: How to initialize array in class


This is neat and smart! Thanks

Closed Thread


Similar C / C++ bytes