473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to initialize array in class

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

Mar 3 '06 #1
9 24717
* ww*******@gmail.com:
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.

--
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?
Mar 3 '06 #2
>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++\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'

Mar 3 '06 #3
* 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++\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'


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?
Mar 3 '06 #4
ww*******@gmail.com 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[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
Mar 3 '06 #5
ww*******@gmail.com 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[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
Mar 3 '06 #6
ww*******@gmail.com 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[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
Mar 3 '06 #7
Since m_b is a const in array, that means there is no appropriate
constructor using VC6?

Mar 3 '06 #8
shellux wrote:
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++\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'


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

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

Cheers! --M

Mar 3 '06 #9
This is neat and smart! Thanks

Mar 5 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Kyle | last post by:
Hi, Is it possible to initialize a constant memeber array in a class? I tried several syntax but all failed. class A { public: A(); ~A(); private:
4
by: Mark Hannon | last post by:
I am trying to initialize an array only once so it can be seen & used by any functions that need it. As I understand it, if a variable is declared by itself outside of any functions, its scope is...
2
by: slack_justyb | last post by:
Hello, I'm trying to figure the best way of doing the following. *I need an array of strings that are globally accessable from within 'arrayhere.h' *I need that array of strings to not change...
5
by: Jim Langston | last post by:
What I want to do: have a vector of ints in my class initialized with 0 to 499 which will later be pushed/popped out of the vector by instances. What I have: class CParticleStream // Yes, I...
3
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
18
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and...
15
by: thinktwice | last post by:
char a = { 0 } is it ok?
8
by: aaragon | last post by:
Hi, just a very simple question. I was wondering what is the most efficient way of initializing an array. I have a very simple class that wraps an array to provide bound checking, something like...
11
by: Bob Altman | last post by:
Hi all, I have a class that contains a member variable that is an array of class instances: class MyClass { private: SomeClass m_someClass; SomeClass m_arrayOfClasses; };
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.