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

Assignment Help:

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


Related Discussions:- Which constructor gets called while i create an array of

Advanced, how to create a shopping cart

how to create a shopping cart

I need quantitative trading platform, Project Description: Need someone ...

Project Description: Need someone to prepare a trading platform and sophisticated trading strategy. Must have knowledge and experience in most of these: QuickFIX, Esper, Quan

Introduction of computer and programming concept, Classify computer system ...

Classify computer system according to capacity. How they are different from computers according to the classification of technology. Provide comparative study also.

Define constructors-extract and insert operators, For your class to work pr...

For your class to work properly, you'll need to define appropriate constructors, extract and insert operators, and of course arithmetic operators. (If you wanted to use it as a gen

Car rental project, I need a project on car rental system using c programmi...

I need a project on car rental system using c programming only of college level

Difference between mutex and binary semaphore, Semaphore is used to synchro...

Semaphore is used to synchronize processes. whereas mutex is used to give synchronization among threads running in the similar process.

Program for function in cpp, Question Write a program using c++ for the...

Question Write a program using c++ for the above function , at a= 1000, b=10 -3 ,10 -2 ,10 -4

Structures of flowchart, Ask Draw a flowchart that print all even numbers f...

Ask Draw a flowchart that print all even numbers from 2 until 10

Skilled programmer is required to build a standalone module, A skilled prog...

A skilled programmer is required to build a standalone module to extract, parse and store in database tables, word count data from web pages and RSS feeds. The module can take R

Explain pros and cons of cpp as a programming language, Write a 3-4 page pa...

Write a 3-4 page paper (350 words per page) in APA format detailing the development and use of C++ in academia and in industry. Explain the pros and cons of C++ as a programming la

3/15/2013 5:58:52 AM

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?

 

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd