Interview Question for Financial Software Developers






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

1. Copy-Constructor without 'const' compiles.
2. Copy-Constructor without 'const' is called.
3. 'const' in CC prevents passd object from being modified.

Nothing else.

- Maninder Singh June 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

So that the constructor do not change the object from which copy is being made.

- Cookie May 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Traditionally, const arg to a function signifies that the value passed as const wont be altered within the function (if alteration is attempted, code wont compile).

It is a compilation error to not have const in a constructor that accepts the same object's reference (aka copy constructor). Having a const reference of same object as the only non-defaulted argument is the signature, which C++ compiler uses to qualify a copy constructor and invoke it for copying between objects.

Lamely said, its to avoid compiler error.

- Nik May 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I agree that it is a compilation error to not have const in a copy constructor. But I wonder why? what is the reason behind this choice of the C++ compiler? Can it define the signature being without the const?

- trojanhorse May 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks. I agree that it is a compilation error to not have const in a copy constructor. But I wonder why? what is the reason behind this choice of the C++ compiler? Can it define the signature being without the const?

- trojanhorse May 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks. I agree that it is a compilation error to not have const in a copy constructor. But I wonder why? what is the reason behind this choice of the C++ compiler? Can it define the signature being without the const?

- trojanhorse May 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't think you will get a compilation error if you don't have a const in a copy constructor (at least in vc2008 it's not the case).
IMO, using const in a copy ctor makes it ready to take both const and non-const arguments. Without const, the copy ctor can only take non-const arguments.
e.g.
Foo::Foo(Foo&){...} // ctor without const
const Foo a;
Foo b(a) //error

- Anonymous May 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good Answer: using const in a copy ctor makes it ready to take both const and non-const arguments

- Creation May 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess you sometimes need to take a const object as input, e.g., the unnamed return value from a function. so it makes sense to accept const input.

- trojanhorse May 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

But, a constructor without const is not called a copy constructor, right?

- trojanhorse May 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

But, a constructor without const is not called a copy constructor, right?

- trojanhorse May 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Maninder Singh. You rock man...
what do you think all these people have been writing so far...holy crap! OMG...help this poor soul...Jesus!

- assHo August 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The copy constructor takes a reference to a const parameter. It is const to guarantee that the copy constructor doesn't change it, and it is a reference because a value parameter would require making a copy, which would invoke the copy constructor, which would make a copy of its parameter, which would invoke the copy constructor, which ...

- leakymemory September 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Const is not required in copy constructor, rather it is prefered to have the argument const. The basic reason behind this should be that because the temorary objects created in various operation can be passed to copy constructor.

- Mustafa Hussain November 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

const has no influence in copy constructor except that it would be resolved at compile time by the compiler. Only thing of importance is passing the argument by reference, if not passed by reference your call to copy constructor will never come back. It would be creating copy for the passed argument, then a temp copy for the copy, another copy of the temp copy and so on...

- ac November 14, 2009 | 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