Interview Question






Comment hidden because of low score. Click to expand.
0
of 0 vote

using the initializer list is faster. can be optimized by the compiler better I believe

- NewStart April 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not exactly sure of the performance impact by using member initalizer syntax for built in data types. But in the case of Objects(i mean composition.. i composed in the class), the first one is faster, since, in case 1, the object is created by invoking the copy constructor where as in case 2, the object i is initially created using the default constructor, then initialised using the copy constructor.

For built in data types, something like this
int i
i =j

int i=j

For objects.
Object i(j)

Object i;
i=j;

- Anonymous April 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dude, they generate _identical_ code on every C++ compiler I have access to. Don't make the mistake of confusing semantics with implementation.

- Anonymous April 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Really!! I think initializer list uses copy constructor and call to assignment operator will be used in class B....
Please correct me If I am wrong.....

- Anonymous April 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess this answers your question

Thanks
RaghuC

- Anonymous April 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For built-in type data, they will not be implicitly initialized in the initialization phase. So, the performance is equivalent when they are initialized in the initialization list or the constructor functions body.

- Anonymous May 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Initialiser list initialization is faster.
Reason:It initialise 'i' using copy constructor.(one function call)
In the other case ,i constructor is called then a assignment operator is called again to assign..(two function calls)

- Karthik Raj June 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

found this in one website:

class Qux
{
        public:
                Qux() : _foo( "initialize foo to this!" ) { }
                // This is nearly equivalent to 
                // Qux() { _foo = "initialize foo to this!"; }
                // but without the extra call to construct an empty string

        private:
        std::string _foo;
};

So i guess initialization Lists is faster because, it wont call constructor for int( ); and then the operator i=j;
It will directly call i(j);

- unicorn August 05, 2010 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More