Which constructor gets called while i create an array of , C/C++ Programming

Which constructor gets called while I create an array of Fred objects?

Posted Date: 3/15/2013 5:58:17 AM | Location : United States





A: Fred''s default constructor

class Fred { public: Fred();

...

};

int main()

{

Fred a[10];  calls the default constructor 10 times

Fred* p = new Fred[10]; calls default constructor 10 times

...

}

If your class doesn''t contain a default constructor, you''ll get a compile-time error while you try to create an array by using the above simple syntax:

class Fred {

public:

Fred(int i, int j); suppose there is no default constructor

...

};

int main()

{

Fred a[10]; ERROR: Fred doesn''t contain a default constructor

Fred* p = new Fred[10]; ERROR: Fred doesn''t contain a default constructor

...

}

Though, even if your class already contain a default constructor, you must try to use std::vector instead of an array (arrays are evil). Std::vector allows you decide to use any constructor, not only the default constructor:

#include int main()

{

std::vector a(10, Fred(5,7));  the 10 Fred objects in std::vector a will be initialized along with Fred(5,7)

...

}

Although you have to use a std::vector instead of an array, there are times while an array may be the right thing to do, and for those, you may need the "explicit initialization of arrays" syntax. Here''s how:

class Fred {

public:

Fred(int i, int j);  assume there is no default constructor

...

};

int main()

{

Fred a[10] = {

Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7), // The 10 Fred objects are

Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7) // initialized using Fred(5,7)

};

...

}

Certainly you don''t contain to do Fred(5,7) for every entry you can put in any numbers you wish, even parameters or other variables.

Lastly, you can use placement-new to initialize manually the elements of the array. Warning: it''s unattractive: the raw array can''t be of type Fred, so you''ll require a bunch of pointer-casts to do things such as compute array index operations. Warning: its compiler- and hardware-dependent: you''ll require making sure the storage is aligned along with an alignment i.e. at least as strict as is needed for objects of class Fred. Warning: it''s boring to make it exception-safe: you''ll require to manually destructing the elements, by including in the case while an exception is thrown part-way through the loop which calls the constructors. But if you really wish to do it anyway, read up on placement- new. (BTW placement-new is like magic which is used inside of std::vector. The difficulty of getting everything right is still another reason to use std::vector.)

By the way, did I ever denote that arrays are wicked? Or did I denote that you have to use a std::vector unless there is a compelling cause to use an array?

 

Posted by | Posted Date: 3/15/2013 5:58:52 AM


Related Discussions:- Which constructor gets called while i create an array of , Assignment Help, Ask Question on Which constructor gets called while i create an array of , Get Answer, Expert's Help, Which constructor gets called while i create an array of Discussions

Write discussion on Which constructor gets called while i create an array of
Your posts are moderated
Related Questions
Project Overview A certain financial institution (bank) wishes to promote its new business products/services by conducting road shows in rural areas. Their aim is to encourage peop


Explain each of the algorithms in a way that would be understandable to an intelligent person who is not familiar with programming. You should not use any code (or even pseudo code

Selecting Array Elements Implement the following C++ code in assembly language, using the block-structured .IF and .WHILE directives. Assume that all variables are 32-bit signed in

though the ascii is called 7-bit code. why do we use 8-bits to represent a character?

write a program to calculate the cuboid

C program to print fibonacci series: int fibo(long int); void main() {                 long int a=0,n;                 printf ("how many terms\n");

em Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using ''Content here, content here'', making it look like readable English. Many desktop publis

L is a text and can be composed of any of the characters {, }, (, ) , and P, where P will represent the instruction. L will contain single spaced characters where each character

Question 1 Explain the following Object Oriented Concepts with suitable examples for each Question 2 Explain the following Object Oriented Methodologies Question 3 Descr