Hello,
I've been loooking for info about this issue, but I still can't find.
If I create an array of objects, are the constructors called in some
predefined order, or it is compiler-dependent?
For instance, having a class "A":
A a[10];
What I have seen is that constructor for a[0] is called, and then
operator = for the rest, but I supposed it is not a rule.
Can I trust that the element at position 0 of the array will always be
constructed first? I suppose I can't, but I would like to know your
opinions.
Thanks a lot,
Luis 5 1692
I think you can test it by this way:
Declare a global variable which is initialize to 0,for example,int i=0;
In the Constructor ,you can asign i to a class member variable,for
example,num=i++;
And then you can print the num variables of objects in the array,then
you can see which one is first constructed.
kalki70 写道:
Hello,
I've been loooking for info about this issue, but I still can't find.
If I create an array of objects, are the constructors called in some
predefined order, or it is compiler-dependent?
For instance, having a class "A":
A a[10];
What I have seen is that constructor for a[0] is called, and then
operator = for the rest, but I supposed it is not a rule.
Can I trust that the element at position 0 of the array will always be
constructed first? I suppose I can't, but I would like to know your
opinions.
Thanks a lot,
Luis
#include <stdio.h>
int g=0;
class mycls
{
public:
int i;
mycls()
{
i=g++;
}
};
int main()
{
mycls myarray[10];
printf("array construct order:");
for(int j=0;j<10;j++)
printf("%d",myarray[j].i);
return 0;
} xm******@gmail.com 写道:
I think you can test it by this way:
Declare a global variable which is initialize to 0,for example,int i=0;
In the Constructor ,you can asign i to a class member variable,for
example,num=i++;
And then you can print the num variables of objects in the array,then
you can see which one is first constructed.
kalki70 写道:
Hello,
I've been loooking for info about this issue, but I still can't find.
If I create an array of objects, are the constructors called in some
predefined order, or it is compiler-dependent?
For instance, having a class "A":
A a[10];
What I have seen is that constructor for a[0] is called, and then
operator = for the rest, but I supposed it is not a rule.
Can I trust that the element at position 0 of the array will always be
constructed first? I suppose I can't, but I would like to know your
opinions.
Thanks a lot,
Luis
kalki70 wrote:
For instance, having a class "A":
A a[10];
What I have seen is that constructor for a[0] is called, and then
operator = for the rest, but I supposed it is not a rule.
Can I trust that the element at position 0 of the array will always be
constructed first? I suppose I can't, but I would like to know your
opinions.
>From the C++ standard
12.6.3
When an array of class objects is initialized (either explicity or
implicity), the constructor shall be called for each element of the
array, following the subscript order; see 8.3.4 [Note: destructors for
the array elements are called in reverse order of their constructions]
Second:
A a[10];
Calls the default constructor on each of on all of the array elements
in order. For a simple test program, demonstrating the order and which
constructor is called:
#include <iostream>
class testing {
public:
testing() {
std::cout << "Default Constructed at " << this << std::endl;
}
~testing() {
std::cout << "Destructed at " << this << std::endl;
}
};
int main()
{
testing mytests[10];
return 0;
} xm******@gmail.com ha escrito:
I think you can test it by this way:
Declare a global variable which is initialize to 0,for example,int i=0;
In the Constructor ,you can asign i to a class member variable,for
example,num=i++;
And then you can print the num variables of objects in the array,then
you can see which one is first constructed.
Thanks, but I DON'T want to test it. Doing so may just show me a
specific compiler implementation and there can be multiple
implementations.
Luis
>
kalki70 写道:
Hello,
I've been loooking for info about this issue, but I still can't find.
If I create an array of objects, are the constructors called in some
predefined order, or it is compiler-dependent?
For instance, having a class "A":
A a[10];
What I have seen is that constructor for a[0] is called, and then
operator = for the rest, but I supposed it is not a rule.
Can I trust that the element at position 0 of the array will always be
constructed first? I suppose I can't, but I would like to know your
opinions.
Thanks a lot,
Luis
Todd Gardner ha escrito:
kalki70 wrote:
For instance, having a class "A":
A a[10];
What I have seen is that constructor for a[0] is called, and then
operator = for the rest, but I supposed it is not a rule.
Can I trust that the element at position 0 of the array will always be
constructed first? I suppose I can't, but I would like to know your
opinions.
From the C++ standard
12.6.3
When an array of class objects is initialized (either explicity or
implicity), the constructor shall be called for each element of the
array, following the subscript order; see 8.3.4 [Note: destructors for
the array elements are called in reverse order of their constructions]
That's the answer I needed!!! Thanks a lot!!!
Best regards,
Luis
Second:
A a[10];
Calls the default constructor on each of on all of the array elements
in order. For a simple test program, demonstrating the order and which
constructor is called:
#include <iostream>
class testing {
public:
testing() {
std::cout << "Default Constructed at " << this << std::endl;
}
~testing() {
std::cout << "Destructed at " << this << std::endl;
}
};
int main()
{
testing mytests[10];
return 0;
}
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Thomas Hede Jensen |
last post by:
Hi,
I'm currently working on a project in which I link to a library where the
function definitions takes parameters of the type: "const int *const
*paramName"
As far as I can tell from the...
|
by: Abdullah Kauchali |
last post by:
Hi folks,
Can one rely on the order of keys inserted into an associative Javascript
array? For example:
var o = new Object();
o = "Adam";
o = "Eve";
|
by: rusty |
last post by:
Hi,
im currently working on a web app which uses heavy javascript. in one
of the functions, a simple array is created using "var admin_types =
new Array();". This array is not empty, it has a...
|
by: sriram |
last post by:
Hi,
I have been seing a weird problem with db2look tool in db2 8.2 on
Windows 2000 platform.
When i spool the DDL using db2look, it spools the DDL in the ascending
order of database objects...
|
by: subramanian |
last post by:
Suppose I have the following class:(shown only partially. Assume proper
ctors and dtors):
class Date
{
...
static Date temp_date;
static Date another_date;
|
by: Sandro Bosio |
last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long.
I'm trying to achieve the...
|
by: Ramashish Baranwal |
last post by:
Hi,
I want to get a module's contents (classes, functions and variables)
in the order in which they are declared. Using dir(module) therefore
doesn't work for me as it returns a list in...
|
by: David Golightly |
last post by:
Quick question for the gurus out there: in ECMAScript, one can create
a new Array object with a length like so:
var animals = new Array(128);
This creates a new Array object with a "length"...
|
by: Tom P. |
last post by:
I am doing quite a bit of custom painting and it means I have to
create a lot of brushes (think one for every file system object in a
directory) per paint. How expensive is this? Should I find a...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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: 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: 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...
| |