How to initialize array in class

W

www.brook

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
 
A

Alf P. Steinbach

* [email protected]:
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

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.
 
S

shellux

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.


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++\practice\commutytry\communitytry.cpp(3)
: error C2439: 'm_int' : member could not be initialized

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

Alf P. Steinbach

* shellux:
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.


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++\practice\commutytry\communitytry.cpp(3)
: error C2439: 'm_int' : member could not be initialized

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

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.
 
T

Tomás

[email protected] posted:
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
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;

DefaultInitialised() : object() {}

operator (&T)()
{
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
 
T

Tomás

[email protected] posted:
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
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;

DefaultInitialised() : object() {}

operator (&T)()
{
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
 
T

Tomás

[email protected] posted:
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
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;

DefaultInitialised() : object() {}

operator (&T)()
{
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
 
W

www.brook

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

mlimber

shellux said:
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.


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++\practice\commutytry\communitytry.cpp(3)
: error C2439: 'm_int' : member could not be initialized

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

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

http://groups.google.com/group/comp.lang.c++/msg/4675a9bd905b06c0

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top