473,398 Members | 2,188 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,398 software developers and data experts.

creating static array

Hi,

I want to create a static array whose size is also a const
member of the class. Something like:

// A.h
class A {
private:
static int size = 0;
static int array[size];
};

Then I need to define the static array in A.cpp like:

int A::array[A::size];

However, the compiler complains that A::size is a private
and cannot be accessed. So how can I define such a static
array without making the size of the array public ?

Thanks,
Rahul

Jul 19 '05 #1
3 35266
Rahul Joshi wrote:
...
I want to create a static array whose size is also a const
member of the class. Something like:

// A.h
class A {
private:
static int size = 0;
static int array[size];
};

Then I need to define the static array in A.cpp like:

int A::array[A::size];

However, the compiler complains that A::size is a private
and cannot be accessed. So how can I define such a static
array without making the size of the array public ?
...


Aside from missing 'const' specifier and array size being zero, I don't
see any problems with this code. (Please, always post _real_ code, i.e.
the code you are actually trying to compile.)

The error message is most likely caused by a bug in your compiler.
According to 11/5 access control is not supposed to restrict access to
'A::size' in the above member definition.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #2
i think you just make a mistake when you declare
static int array[size] ;
because std c++ doesn't support "dynamic" declare , you know size is not a
const number, so this will generate a mistake. and i compile it in dev-c++
4.9.8.0 with winme,
it just appear "4 D:\source code\stl_c++\statictest.h
size of member `array' is not constant"
so if you want to achive this ,i think the best way is to use stl: vector.
"Rahul Joshi" <ru*****@students.uiuc.edu>
??????:Pi*************************************@ux7 .cso.uiuc.edu...
Hi,

I want to create a static array whose size is also a const
member of the class. Something like:

// A.h
class A {
private:
static int size = 0;
static int array[size];
};
Then I need to define the static array in A.cpp like:

int A::array[A::size];

However, the compiler complains that A::size is a private
and cannot be accessed. So how can I define such a static
array without making the size of the array public ?

Thanks,
Rahul



Jul 19 '05 #3


On Thu, 17 Jul 2003, Andrey Tarasevich wrote:
Rahul Joshi wrote:
...
I want to create a static array whose size is also a const
member of the class. Something like:

// A.h
class A {
private:
static int size = 0;
static int array[size];
};

Then I need to define the static array in A.cpp like:

int A::array[A::size];

However, the compiler complains that A::size is a private
and cannot be accessed. So how can I define such a static
array without making the size of the array public ?
...


Aside from missing 'const' specifier and array size being zero, I don't
see any problems with this code. (Please, always post _real_ code, i.e.
the code you are actually trying to compile.)

The error message is most likely caused by a bug in your compiler.
According to 11/5 access control is not supposed to restrict access to
'A::size' in the above member definition.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP


Sorry. That was some code I made up. I was trying to define a static array
of pointers-to-member functions of the class, and the size of this array
is a static const int. Apparently, my syntax was wrong. As suggested in
C++ FAQ, I used a typedef for the pointer-to-member type, and it compiled
correctly. Thanks.

Rahul

Jul 19 '05 #4

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

Similar topics

6
by: Thomas Matthews | last post by:
Hi, How do I create a const table of pointers to member functions? I'm implementing a Factory pattern (or jump table). I want to iterate through the table, calling each member function until a...
4
by: Bill | last post by:
I would like to create a static array of classes (or structs) to be used in populating name/value pairs in various WebForm drop down list boxes, but am not quite sure of the construct (or rather to...
31
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are...
13
by: Justcallmedrago | last post by:
How would you declare and assign a variable inside a function THAT HAS THE NAME OF A PARAMETER YOU PASSED example: when you call createvariable("myvariable") it will declare the variable...
3
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
6
sammyboy78
by: sammyboy78 | last post by:
I'm trying to display my array of objects in a GUI. How do I get JLabel to refer to the data in my objects? I've read my textbook and some tutorials online I just cannot get this. Plus all the...
11
by: Matthew Wells | last post by:
Hello. I have figured out how to create an instance of an object only knowing the type by string. string sName = "MyClassName"; Type t = Type.GetType(sName); Object objNew =...
9
by: Martin Racette | last post by:
Hi, I new to C#, and I would like to know how I can create an array that I do not know in advance how many item will be placed there -- Thank You in Advance Merci a l'avance Martin
1
by: lilsugaman | last post by:
Hi I have created an Inventory program that displays the product name, item number, how many units, and it's price and at the end displays the totals, I have to modify the program so that it will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
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...

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.