473,793 Members | 2,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initialization of an array

How can you initialize an array, in the initialization list of a
constructor ??

SomeClass
{
public:
SomeClass() : *init here* { }
private:
int some_array[2];
};

Feb 25 '07 #1
15 3380
jamx wrote:
How can you initialize an array, in the initialization list of a
constructor ??

SomeClass
{
public:
SomeClass() : *init here* { }
private:
int some_array[2];
};
You cannot.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 25 '07 #2
jamx wrote:
How can you initialize an array, in the initialization list of a
constructor ??

SomeClass
{
public:
SomeClass() : *init here* { }
private:
int some_array[2];
};
You can't do it directly, but you can use some like this:

class Init
{
public:
Init (int (& array ) [2])
{
array [0]= 42;
array [1]= 43;
}
};

class SomeClass : private Init
{
public:
SomeClass() : Init (some_array) { }
private:
int some_array[2];
};

--
Salu2
Feb 25 '07 #3
* Victor Bazarov:
jamx wrote:
>How can you initialize an array, in the initialization list of a
constructor ??

SomeClass
{
public:
SomeClass() : *init here* { }
private:
int some_array[2];
};

You cannot.
Well, you can zero-initialize it.

--
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?
Feb 25 '07 #4
jamx wrote:
How can you initialize an array, in the initialization list of a
constructor ??

SomeClass
{
public:
SomeClass() : *init here* { }
private:
int some_array[2];
};
I would simply initialize it in the constructor's body (actually, member
initialization is the constructor's bread-n-butter):
SomeClass() {
// *init some_array here */
}
Feb 26 '07 #5

Julian Albo wrote:
>
>How can you initialize an array, in the initialization list of a
constructor ??

SomeClass
{
public:
SomeClass() : *init here* { }
private:
int some_array[2];
};

You can't do it directly, but you can use some like this:

class Init
{
public:
Init (int (& array ) [2])
{
array [0]= 42;
array [1]= 43;
Note: it is assignment rather than initialization: "default ctor +
operator=".

--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Feb 26 '07 #6
On 25 feb, 22:29, "Alf P. Steinbach" <a...@start.now rote:
* Victor Bazarov:
jamx wrote:
How can you initialize an array, in the initialization list of a
constructor ??
SomeClass
{
public:
SomeClass() : *init here* { }
private:
int some_array[2];
};
You cannot.

Well, you can zero-initialize it.

--
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?
Thats what i want, to initialize them with zero ;-)

Feb 26 '07 #7
* jamx:
On 25 feb, 22:29, "Alf P. Steinbach" <a...@start.now rote:
>* Victor Bazarov:
>>jamx wrote:
How can you initialize an array, in the initialization list of a
constructo r ??
SomeClass
{
public:
SomeClass() : *init here* { }
private:
int some_array[2];
};
You cannot.
Well, you can zero-initialize it.
Thats what i want, to initialize them with zero ;-)
Please don't quote signatures -- corrected.

In standard C++ you just write

struct SomeClass
{
SomeClass(): some_array() {}
int some_array[2];
};

Some compilers (notably old Visual C++, IIRC) don't support that, i.e.
they're not standard-conforming, but for such compilers you can employ
the artificial base class trick:

struct SomeClassMember s
{
int some_array[2];
};

struct SomeClass: SomeClassMember s
{
SomeClass(): SomeClassMember s() {}
};

However, no matter whether your compiler is standard-conforming in this
respect or not, you're probably much better off using a std::vector than
a raw array, i.e.

struct SomeClass
{
SomeClass(): some_array(2) {}
std::vector<int some_array;
};

--
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?
Feb 26 '07 #8
On 26 feb, 09:41, "Alf P. Steinbach" <a...@start.now rote:
* jamx:
On 25 feb, 22:29, "Alf P. Steinbach" <a...@start.now rote:
* Victor Bazarov:
>jamx wrote:
How can you initialize an array, in the initialization list of a
constructor ??
SomeClass
{
public:
SomeClass() : *init here* { }
private:
int some_array[2];
};
You cannot.
Well, you can zero-initialize it.
Thats what i want, to initialize them with zero ;-)

Please don't quote signatures -- corrected.

In standard C++ you just write

struct SomeClass
{
SomeClass(): some_array() {}
int some_array[2];
};

Some compilers (notably old Visual C++, IIRC) don't support that, i.e.
they're not standard-conforming, but for such compilers you can employ
the artificial base class trick:

struct SomeClassMember s
{
int some_array[2];
};

struct SomeClass: SomeClassMember s
{
SomeClass(): SomeClassMember s() {}
};

However, no matter whether your compiler is standard-conforming in this
respect or not, you're probably much better off using a std::vector than
a raw array, i.e.

struct SomeClass
{
SomeClass(): some_array(2) {}
std::vector<int some_array;
};

--
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?
It's for BSD/UNIX, so i'm using gcc. I will try out this Someclass() :
some_arra() {} construction, thanks.

I agree, that most of the time a vector is the better choise. But
since pipe() requires an "int fildes[2]", i will use an array.

Feb 26 '07 #9
On 26 Feb, 12:31, "jamx" <debraban...@gm ail.comwrote:
I agree, that most of the time a vector is the better choise. But
since pipe() requires an "int fildes[2]", i will use an array.
As it happens, none of that prevents you using a vector if you want
to.

pipe might say it takes an int fildes[2] but C and C++ don't let you
be that precise in function declarations. In fact, pipe takes just an
int* and it is entirely your responsibility to make sure that that
pointer points to the first element of an array with (at least) 2
elements. You can equally well do

vector<intv(2);
pipe(&v[0]);

which allows you to continue to use containers in your own code. Raw
arrays and pointers don't need to propogate outside the API you are
using.

Gavin Deane
Feb 26 '07 #10

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

Similar topics

1
3015
by: Piotr Sawuk | last post by:
just a quick question out of curiosity: how to initialize const arrays? struct srat { long num; ulong den; srat(){} } struct collisions
19
4562
by: Henry | last post by:
I finally thought I had an understanding of multi dimensional arrays in C when I get this: #include <stdio.h> #define max_x 3 #define max_y 5 int array;
6
13386
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
4
9691
by: Stephen Mayes | last post by:
I have initialized an array like this. const char matrix = { {0, 1, 2, 3}, {0, 1, 2}, {0, 1} }; gcc, (with no options set,) errors unless I specify
10
8563
by: utab | last post by:
Dear all, Can somebody direct me to some resources on the subject or explain the details in brief? I checked the FAQ but could not find or maybe missed. Regards,
5
24322
by: toton | last post by:
Hi, I can initialize an array of class with a specific class as, class Test{ public: Test(int){} }; Test x = {Test(3),Test(6)}; using array initialization list. (Note Test do NOT have a default ctor). Is it possible to do so in the class parameter initialization using specific ctor?
23
3664
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for the built-in types, the value is usually garbage. Is this right? However, I'm a bit confused about value-initialization, when does it happen, and what kind of values are assigned to objects?
3
4912
by: jaime | last post by:
Hi all. The source code download bundle for "Beginning C: From Novice to Professional, Fourth Edition" (ISBN: 1590597354) (Horton/Apress) contains a C source file (program9_09.c) which contains several instances of the following type of idiom: /* Program 9.9 REVERSI An Othello type game */ const int SIZE = 6;
5
9863
by: codeGhost | last post by:
I've been trying to ignore this issue for a while now, but I've come to the point in my code where I can't do so anymore. (For those of you who are wondering, this is NOT a homework question). Platform: VC++ 2002 System: Windows XP, sp3 ::First, here's the code in question:: char * cSection (const char* data, int start, int finish) {
0
9671
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10212
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10161
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.