473,395 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 15680
Hello,

"RCAJ" <rc******@teleline.es> wrote in message
news:Dl***********************@telenews.teleline.e s...
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
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
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
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
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...
15
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...
9
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
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...
4
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...
1
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;...
11
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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
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...
0
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...

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.