473,809 Members | 2,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initialize an static array in a class

How can I do the following?

class C{
private:
static const char c[3] = {1,2,3};
//...
};

The compiler says it can't be done like that.

Jul 22 '05 #1
6 15708
Hello,

"RCAJ" <rc******@telel ine.es> wrote in message
news:Dl******** *************** @telenews.telel ine.es...
How can I do the following?

class C{
private:
static const char c[3] = {1,2,3};
//...
};

The compiler says it can't be done like that.

Static variables should be initialized from outside class.

--
Elias
Jul 22 '05 #2
RCAJ wrote:
How can I do the following?

class C{
private:
static const char c[3] = {1,2,3};
//...
};

The compiler says it can't be done like that.


Yeah, those up-tight compilers can be really retentive, you know?
class C
{
static const char c[ 3 ];
};

const char C::c[ 3 ] = { 1, 2, 3 };

Jul 22 '05 #3
Static variables should be initialized from outside class.

--
Elias


how?

I just want const values for that array. How can I initialize it so that
when I create an object the values are ready to be used without
initializing them in the constructor?

Jul 22 '05 #4
Jeff Schwab escribió:
RCAJ wrote:
How can I do the following?

class C{
private:
static const char c[3] = {1,2,3};
//...
};

The compiler says it can't be done like that.

Yeah, those up-tight compilers can be really retentive, you know?
class C
{
static const char c[ 3 ];
};

const char C::c[ 3 ] = { 1, 2, 3 };


Thanks. It already works! But I've got a question: why can I get access
to a private atribute outside the class?

Jul 22 '05 #5
"Alexandros " <em***@company. com> wrote...
[...]
Thanks. It already works! But I've got a question: why can I get access
to a private atribute outside the class?


Probably because it's _private_...
Jul 22 '05 #6
Alexandros wrote:
class C{
private:
static const char c[3] = {1,2,3};
//...
};

<snip/>
class C
{
static const char c[ 3 ];
};

const char C::c[ 3 ] = { 1, 2, 3 };


Thanks. It already works! But I've got a question: why can I get access
to a private atribute outside the class?


You're not really accessing it, you're still defining and initializing
it. The semantics are the same as for any private method. You can
define it outside the class. The defintion doesn't count as an access.
You're not actualling calling the method, or in this case, accessing
the variable.

-Jeff

Jul 22 '05 #7

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

Similar topics

2
14133
by: Makis Papapanagiotou | last post by:
Hello, I really don't understand the simplest thing at the moment. I have a static array of pointers to a class . How do we initialize it? class classA { public: int x1; };
4
2301
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); };
2
1932
by: zoe | last post by:
Is it possible to initialize a non-static const array class-member of a fundamental type? Something like: class Foo{}{ public: Foo(); private: const int myArray;
2
57998
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 once initialized 'const' *I would like to initialize said array with a one liner, but if not something extremly easy to read. *I need to know how many elements there are in the array so I can stop
15
4908
by: Charles Sullivan | last post by:
Assume I have a static array of structures the elements of which could be any conceivable mixture of C types, pointers, arrays. And this array is uninitialized at program startup. If later in the program I wish to return this array to its startup state, can this be accomplished by writing binary zeroes to the entire memory block with memset(). E.g., static struct mystruct_st { int x1;
9
27385
by: subramanian | last post by:
I am a beginner in C++. Suppose I want to build a class: I have given below the starting code: class Date { int day, month, year; static Date default_date; };
1
4269
by: Vol | last post by:
Hi, Group, I want to initialize a 2D static array in function 'foo ()', where 'foo' is called by another function a lot of times. I list my implementation below but I think there are better solutions , thanks void foo() { int i, j;
4
8377
by: Bram Kuijper | last post by:
Hi all, as a C++ newbie, I got some question on the initialization of static reference data members. Since it isn't possible to initialize static members of a class in the constructor, I should initialize them in advance. However, the following code, in which I first produce two classses and then try to assign a reference of the first class to a static data member of the second class doesn't work. It gives the following compiler error:
1
3389
by: flowstudioLA | last post by:
I have a template class object that I use as a mesaging queue between threads. I use it as a static object that I initialize like so: foo.h class foo{ static LFQueue<const char*,100lfqMyQueue; }; foo.cpp LFQueue<const char*,100Foo::lfqMyQueue;
11
2338
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
9601
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
10115
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
9198
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...
1
7653
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 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...
0
6881
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();...
0
5550
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...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
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
3
3013
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.