Interview Question






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

Yes you can convert one object into another object..
For example:
you have a class B constructor B(int b) and class A constructor A(int a1, int a2).
Now you want to convert A object into B object. So simply write a conversion operator in class A.
i.e. operator B(){const A a){
return B( a.a1);
}

- tito March 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You need to use reinterpret_cast.

- Venkat April 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I think the question is about explicit conversion operators.
Like that:

class A
{
  private:
  double d;

  public:
  A(double x):d(x){}
  operator double()
  {return d;}
}

In "main":

A a(2.0);
double dd;
dd=a;

- sergey.a.kabanov January 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is this supposed to mimic the CAST? If so, however, every time when do conversion there must be a B object, instead of using B class type. I know we can't make operator() static, but is there some other way? Thanks!

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

type conversion operator is especially useful if some mathematics operations are to achieve.

class Foo; //wrapper of double
Foo a;
float b;
Foo c = a + b;

- peter tang October 09, 2012 | 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