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:
const int a[4];
};
A::A():a={0,1,2 ,3}
{
} 13 14841
Kyle wrote: 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: const int a[4]; };
A::A():a={0,1,2 ,3} { }
You can only initialize it inside the function body, IIRC. Eg: with a for
loop, copying from that constant array, or with memcpy - if the arrays type
allows it.
--
Attila aka WW
"Kyle" <zh**********@h otmail.com> wrote in message
news:40******** @dnews.tpgi.com .au... Hi, Is it possible to initialize a constant memeber array in a class?
No, not a nonstatic one. Why do you want to?
-Mike
Kyle wrote: Is it possible to initialize a constant member array in a class? I tried several syntax but all failed.
struct Array {
int a[4];
};
class A {
private:
// representation
const Array array;
public:
A(const Array& a): array(a) { }
~A(void) { };
};
int main(int argc, char* argv[]) {
Array array = {{0, 1, 2, 3}};
A a(array);
return 0;
}
Attila Feher wrote: Kyle wrote: 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: const int a[4]; };
A::A():a={0,1,2 ,3} { } You can only initialize it inside the function body, IIRC.
No. Initialization of members can only be done in the initializer list.
Eg: with a for loop, copying from that constant array, or with memcpy - if the arrays type allows it.
He doesn't want to copy something _from_ his array, but rather _to_ it,
which isn't possible, since it's constant.
Rolf Magnus wrote: Attila Feher wrote:
Kyle wrote: 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: const int a[4]; };
A::A():a={0,1,2 ,3} { }
You can only initialize it inside the function body, IIRC.
No. Initialization of members can only be done in the initializer list.
Eg: with a for loop, copying from that constant array, or with memcpy - if the arrays type allows it.
He doesn't want to copy something _from_ his array, but rather _to_ it, which isn't possible, since it's constant.
I have missed the const.
--
Attila aka WW
I just need an array with constant content. Yes, probably I need to add
static, since this array is identical for all instances. But the following
code still cannot be complied. (gcc3.3.1)
I am really confused with these 'const' problems.
class A
{
static const int a[4]={0,1,2,3};
...
};
"Mike Wahler" <mk******@mkwah ler.net> дÈëÓʼþ
news:AK******** **********@news read2.news.pas. earthlink.net.. . "Kyle" <zh**********@h otmail.com> wrote in message news:40******** @dnews.tpgi.com .au... Hi, Is it possible to initialize a constant memeber array in a class?
No, not a nonstatic one. Why do you want to?
-Mike
How to do it?
const array cannot be assigned in constructor body.
"Attila Feher" <at**********@l mf.ericsson.se> дÈëÓʼþ
news:bv******** **@newstree.wis e.edt.ericsson. se... Kyle wrote: 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: const int a[4]; };
A::A():a={0,1,2 ,3} { } You can only initialize it inside the function body, IIRC. Eg: with a for loop, copying from that constant array, or with memcpy - if the arrays
type allows it.
-- Attila aka WW
Kyle wrote: I just need an array with constant content. Yes, probably I need to add static, since this array is identical for all instances.
If it's static, it can be initialized.
But the following code still cannot be complied. (gcc3.3.1) I am really confused with these 'const' problems. class A { static const int a[4]={0,1,2,3}; ... };
You cannot intitialize the array within the class, since you first have
to define the constant (the above only declares it), and when defining
it, you can intitialize it, so try:
class A
{
static const int a[4];
//...
};
And in the implementation file:
const int A::a[4] = {0, 1, 2, 3};
Kyle wrote: How to do it? const array cannot be assigned in constructor body.
I have missed the const.
--
Attila aka WW This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: sruojlim |
last post by:
Hi everyone
I'm posting again this question as my system was down at time of the first
posting...well something in the newsgroups was down.
I'd like to know how i should declare a constant as an array with two
dimension. I've tried the following but seems the syntax is somehow wrong.
Can someone help me in doing so or any clue would be fully appreciate.
Here is my bugged code:
|
by: Avner Flesch |
last post by:
Hi,
Do you know how can I intialize an array member:
for example
class A
{
public:
A(int x);
};
|
by: jut_bit_zx |
last post by:
class A
{
public:
A();
virtual ~A(){}
....
private:
int m_iarray;
}
|
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
private string myArray;
|
by: bob |
last post by:
Why doesn't c++ let me initialize static members like this:
class MySound
{
static CSoundManager* g_pSoundManager = NULL;
};
| |
by: nik |
last post by:
hello friends,
a class having two const variables ,how can we initialize these
variables
|
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 use my specific
constructor to initialize the class. How to do it without using malloc?
Something like Point* pt = new Point; I want it to reserve the
space for N points only, and not to call default constructor. I dont
have a default...
|
by: thinktwice |
last post by:
char a = { 0 }
is it ok?
|
by: John Koleszar |
last post by:
Hi all,
I'm porting some code that provides compile-time assertions from one
compiler to another and ran across what I believe to be compliant code
that won't compile using the new compiler. Not naming names here to
remove bias - I'm trying to tell if I'm relying on implementation defined
behavior or if this is a bug in the new compiler.
Consider this stripped down example:
|
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: 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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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: 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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |