473,545 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 35283
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++\st atictest.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*****@studen ts.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
3473
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 non-zero index is returned. Below is my attempt, which generates compiler errors: namespace Reference {
4
4266
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 use structs instead of classes in the array insofar as structs vs. classes appears to be controversial in C# -- with some recommending avoiding...
31
3152
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 some of the most powerful programming tools I have found. Often times as my program grows so do my objects. Often times I look back and see that...
13
2000
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 "myvariable" and then maybe assign it something. myvariable = "this is a real variable"
3
2270
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 these structs. A typical usage case will be as ff (note the code below is Pseudocode and WILL NOT compile) //example structs (I have left out the...
6
3872
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 examples I've seen are creating the information that will be displayed from scratch, while I have to use my previously created classes and add a GUI to it....
11
3716
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 = Activator.CreateInstance(t); This works, but now I need to declare an array like
9
2038
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
1627
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 handle multiple items. Use an array to store the items. The output should display the information one product at a time, including the item number, the...
0
7479
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, 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...
0
7669
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, 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. ...
1
7439
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...
1
5343
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...
0
4962
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...
0
3468
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...
1
1901
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
1
1028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
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...

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.