473,513 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array of objects where the size of an object depends on user input

4 New Member
In c++, I need to have arrays of 256 nodes where the number of references in the nodes of an array is not determined until run time. The number of references is passed in to the constructor of a node. How would I get an array of nodes with each node containing 2 references and other array of nodes with 4, 8, or 16 references in each node?

I could get the size of a node and multiply it by 256 to get the size to pass to malloc. Is this the best way to do this?

I would like to declare an array of nodes and specify the node's constructor parameters in the declaration but I don't see how to do this.

class node
{
public:
unsigned int id;
unsigned short ReferencesUsed;
reference *Reference;
node(unsigned int MaxReferences);
};
node::node(unsigned int MaxReferences)
{
ReferencesUsed = 0;
Reference = new reference [MaxReferences];
id = 0;
};

main
{
unsigned int MaxRefs = 2;
node n2 = node(MaxRefs) [256]; // this is what I want but is not valid
MaxRefs=16;
node n16 = node(MaxRefs) [256];
// other code
}

:confused:
Aug 14 '06 #1
3 2269
Banfa
9,065 Recognized Expert Moderator Expert
I think when declaring an array of objects you have no optiuon but to call the default constructor.

However you could simply have the default constructor create a node with no references and a member function to call to then set the number of references for the node

Expand|Select|Wrap|Line Numbers
  1. node n2[256];
  2.  
  3. for(int i=0; i<256; i++)
  4.     n2.SetNumberOfReferences(2);
  5.  
[/code]
Aug 14 '06 #2
brimp
4 New Member
If I declare an array of nodes before I know the size of each node in the array then how will the compiler know how many bytes should be allocated for the array?
Aug 14 '06 #3
Banfa
9,065 Recognized Expert Moderator Expert
The class node is of fixed size (all classes are of fixed size), the compiler already knows how big it is, all you have to supply is supply the number of references which is used to dynamically allocate memory so this can be done after the node has been instanciated.

Of course you could use templates

Expand|Select|Wrap|Line Numbers
  1. template<int MaxReferences> class node
  2. {
  3. public:
  4.     unsigned int id;
  5.     unsigned short ReferencesUsed;
  6.     reference Reference[MaxReferences];
  7.     node();
  8. };
  9. node::node()
  10. {
  11.     ReferencesUsed = 0;
  12.     id = 0;
  13. };
  14.  
  15. int main()
  16. {
  17.     node<2> n2[256];
  18.     node<16> n16[256];
  19.     // other code
  20. }
  21.  
Aug 15 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
3425
by: Randell D. | last post by:
Folks, I can program fairly comfortably in PHP and can, for the most part using these skills and others that I've picked up over the years manage to read/understand most code in Javascript... so...
12
6972
by: Susan Cranford | last post by:
Please forgive, I have looked at so much info I can't figure out how to put it together even though I know it must be fairly simple. I have an array of input text boxes (txtDOBn) where n is...
38
5148
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
15
5301
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 ...
30
3296
by: questions? | last post by:
say I have a structure which have an array inside. e.g. struct random_struct{ char name; int month; } if the array is not intialized by me, in a sense after I allocated a
23
7367
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
272
13880
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
2
2964
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
152
9736
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
0
7175
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...
0
7391
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
7553
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...
1
7120
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...
1
5100
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...
0
4754
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...
0
3247
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...
0
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1609
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 ...

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.