473,500 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Init an array in a class

Vij
I can do this
int a [] = { 5,6,7,8,9};

but how can I do this inside a class? something like this

class CTest
{
private:
int a [] // Init the array here
};

Sep 6 '05 #1
3 7476
Vij wrote:
I can do this
int a [] = { 5,6,7,8,9};

but how can I do this inside a class? something like this

class CTest
{
private:
int a [] // Init the array here
};

I would do it like this:
// header file
#include <vector>
class CTest
{
public:
CTest();
private:
std::vector<int> a;
}

//source file
namespace{
// solution 1 for short arrays:
std::vector<int> init_a()
{
std::vector<int> result(5);
a.at(0) = 5;
//...
a.at(4) = 9;
return result;
}

// solution 2 for long array:
std::vector<int> init_a()
{
const int a [] = {5, 6, 7, 8, 9};
return std::vector<int>(a, &a[5]); // You might have been looking for
this? vector constructor can take two iterators.
// or, to be very safe:
// return std::vector<int>(a, &a[sizeof(a) / sizeof(int) - 1]);
}

} // unnamed namespace

CTest()
: a(init_a())
{}

- Gabriel
Sep 6 '05 #2
On Tue, 06 Sep 2005 15:30:24 +0400, Vij <vi******@gmail.com> wrote:
I can do this
int a [] = { 5,6,7,8,9};
but how can I do this inside a class? something like this
class CTest
{
private:
int a [] // Init the array here
};


You can't specify explicit initializers for arrays in constructor's
initialization list nor in class declration. You can initialize array as
described before or use static arrays:

struct A
{
static int a[];
};

int A::a[] = {1,2,3};

void main() {
A x;
}
Sep 6 '05 #3
Vij wrote:
I can do this
int a [] = { 5,6,7,8,9};

but how can I do this inside a class?
You can't.
something like this

class CTest
{
private:
int a [] // Init the array here
};


An array needs a size. In the first example, the size can be deduced from
the initializer. But in a class, you can't put an initializer here, so you
must specify a size. Further, non-static array members cannot be
initialized at all in C++.
So the only thing you can do is explicitly specify a size for the array and
assign all the elements in the constructor, like:

class CTest
{
public:
CTest()
{
a[0] = 5;
a[1] = 6;
a[2] = 7;
a[3] = 8;
a[4] = 9;
private:
int a [5];
};

Sep 6 '05 #4

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

Similar topics

5
13262
by: Victor Hannak | last post by:
I have a class that needs to reference a const array in several of its methods. Where should I put the declaration/initialization of this array so that it is only created once when the class is...
10
3855
by: Wylbur via DotNetMonster.com | last post by:
Hello to all of you geniuses, I'm having a problem trying to get an Init handler to fire for a Placeholder control at the initialization phase. I’ve posted this problem to 3 other ASP.NET...
3
1963
by: kk_oop | last post by:
Hi. I recently wrote a simple little template that defines an array that checks attempts to use out of bounds indexes. The only problem is that it does provide the use array style value...
0
2489
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com...
8
2719
by: Grizlyk | last post by:
Good morning. Look here: http://groups.google.com/group/fido7.ru.cpp.chainik/browse_frm/thread/7341aba5238c0f79 and here:...
7
2077
by: recover | last post by:
class Obj { public: Obj(int a){} } class MyContainer { private: Obj m_obj;
4
6988
by: josh | last post by:
Hi, which is the way to init a static array inside a constructor function? i.e. if I had to init ---> int x = {100,200}
4
3691
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
3
15216
by: indiarocks | last post by:
Let's say I have a class A which inherits from B. If I need to call the init function of B, I could just say super(A,self).__init__() in the init function of A. I was wondering how do I call the...
0
7182
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
7232
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6906
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...
0
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.