Hi all. Is there any way to initialize an entire array in the constructor.
The code below does not work. Thanks.
class x {
public:
int array[2][2];
x();
};
x::x() {
array = {{1,2},{3,4}};
} 3 16063
"Allan A." <AA@alphaville. com> wrote in message
news:dt******** *@news4.newsguy .com...
: Hi all. Is there any way to initialize an entire array in the
constructor.
No, unfortunately, not in the current version of the C++ standard.
[ it is being proposed to allow this in the next revision of
the standard, but this is still years away. See for example http://www.open-std.org/jtc1/sc22/wg...2005/n1919.pdf ]
: The code below does not work. Thanks.
:
: class x {
: public:
: int array[2][2];
: x();
:
: };
:
: x::x() {
: array = {{1,2},{3,4}};
: }
The actual (proposed) syntax for an initialization would
look like:
x::x()
: array( {{1,2},{3,4}} )
{ }
The example your provided uses assignment, not initialization,
to set the contents of the array. It could become legal as
well if(/once) the above proposal is integrated in the standard.
Equivalent code can (and has) be written today as individual
assignments to each element of the array.
Ivan
-- http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
On Wed, 22 Feb 2006 15:37:53 +0000, Allan A. wrote: Hi all. Is there any way to initialize an entire array in the constructor. The code below does not work. Thanks.
class x { public: int array[2][2]; x();
};
x::x() { array = {{1,2},{3,4}}; }
You could do this:
x::x() {
static int initData[2][2] = {{1,2},{3,4}};
memcpy(array, initData, sizeof(array));
}
- Jay
Allan A. wrote: Hi all. Is there any way to initialize an entire array in the constructor. The code below does not work. Thanks.
class x { public: int array[2][2]; x();
};
x::x() { array = {{1,2},{3,4}}; }
You could use vector< vector<int> > and do something similar to this: http://groups.google.com/group/comp....75a9bd905b06c0
Then the member could even be const if desired.
Cheers! --M This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: TheKeith |
last post by:
I can't figure out why this doesn't work:
---------------------------------------------
greeting = new Array();
greeting = "hey";
greeting = "bye";
trace(greeting + greeting);
|
by: Terry |
last post by:
Hi,
can someone plz tell me how multidimensional arrays (like a 2-D array)
are stored in memory? Are they like single dimensional arrays? Stored
sequentially in one "row", so to say?
Thanks
Terry
|
by: Charles Banas |
last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to
get some opinions and comments on it, hopefully so i can gain some
sort of insight as to why this works.
at the top of the function (which was translated from Fortran code),
among other heinous and numerous declarations, is this bit:
static float bbuff;
static int bkey;
static int buse;
|
by: Ravi Singh (UCSD) |
last post by:
Hello all
I am trying to use jagged and multi-dimensional arrays in C++.
In C# these work fine
// for jagged arrays
string jaggedArray = new string ;
//for multidimensional arrays
string multidimensionalArray = new string
|
by: jimmij |
last post by:
Hi,
Please look at the code bellow
/*******************/
class ctab
{
private:
static const unsigned n=48;
| |
by: utab |
last post by:
Hi there,
Is there a way to convert a double value to a string. I know that there
is fcvt() but I think this function is not a part of the standard
library. I want sth from the standard if possible.
The thing I am trying to do is to convert a double value to a string
with 8 elements. 8 is fixed because of the files I work with. I will
change this 8 character string with the one(8 character string) already
in the file and so on. But I...
|
by: jayharris |
last post by:
I'm having a ton of trouble initializing a multi-dimensional array
inside a constructor, largely because I don't know the size of the
array until runtime. I have a class that looks like this:
class MyClass
{
public:
const int size;
MyClass( const int );
};
|
by: sg71.cherub |
last post by:
Hi All,
I have encapsulate CvMat of OpenCV into my own matrix class as the
following:
class CVMatrix
{
//== Fields
private:
unsigned m_Width;
|
by: Slain |
last post by:
I need to convert a an array to a multidimensional one. Since I need
to wrok with existing code, I need to modify a declaration which looks
like this
In the .h file
int *x;
in a initialize function:
x = new int;
|
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...
|
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,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |