473,414 Members | 1,596 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,414 software developers and data experts.

Array of objects with non-default constructor possible ?

Hi !

Is it possible to create an array of objects of a class with a constructor that takes some arguments ? If so what is declaration like ?

I mean say I have

class ABC{
int i;
public:
ABC(int m){
i = m;
}
}

To create one object we declare as

ABC *var = new ABC(4);

but if I want to create an array of 100 elements of these objects all initialized to 4, can I do something like :

ABC **var = new ABC[100](4);

???

Thanks
Jan 10 '07 #1
1 17919
horace1
1,510 Expert 1GB
Hi !

Is it possible to create an array of objects of a class with a constructor that takes some arguments ? If so what is declaration like ?

I mean say I have

class ABC{
int i;
public:
ABC(int m){
i = m;
}
}

To create one object we declare as

ABC *var = new ABC(4);

but if I want to create an array of 100 elements of these objects all initialized to 4, can I do something like :

ABC **var = new ABC[100](4);

???

Thanks
the default constructor is called when you create an array - however, you can have a default constuctor that takles parameters so long as you give it default values, e.g.
Expand|Select|Wrap|Line Numbers
  1. class ABC{
  2. int i;
  3. public:
  4.   ABC(int m=4){  // default constructor
  5. i = m;
  6. }
  7. };
  8.  
  9.  
  10. ABC var = ABC(4);
  11. ABC var1[100];
  12. ABC *var2 = new ABC[100];
  13.  
alternativing you can use a vector which allows you to use any constructor, e.g.
Expand|Select|Wrap|Line Numbers
  1. #include <vector>
  2. using namespace std;
  3.  
  4. class ABC{
  5. int i;
  6. public:
  7.   ABC(int m){  // default constructor
  8. i = m;
  9. }
  10. };
  11.  
  12. // create vector with 10 elements initiualised to 4
  13. vector<ABC> var(10, ABC(4));
  14.  
Jan 10 '07 #2

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

Similar topics

2
by: Phil... | last post by:
I am trying to figure out how to create an array that contains objects and not references to objects. I know how to do the latter, but not the former. Any help is appreciated. I have the...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
38
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...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Sam Kong | last post by:
Hello! My question would not be very practical but just out of curiosity. In JavaScript, Array is a subclass of Object. Well maybe not exactly... but sort of... For normal objects, you can...
31
by: siddhu | last post by:
why can't we have array of references.
152
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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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,...
0
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
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...

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.