[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 434
  • Last Modified:

Conversion constructors in C++

What are conversion constructors and why are they called so?
0
Kezia
Asked:
Kezia
  • 4
  • 3
  • 3
  • +1
1 Solution
 
jkrCommented:
A 'conversion ctor' will construc an object from a different data type, and that's why they are called that way. Here's an example from the VC++ docs that illustrates it:

Conversion Constructors
A constructor that can be called with a single argument is used for conversions from the type of the argument to the class type. Such a constructor is called a conversion constructor. Consider the following example:

class Point
{
public:
    Point();
    Point( int );
    ...
};

Sometimes a conversion is required but no conversion constructor exists in the class. These conversions cannot be performed by constructors. The compiler does not look for intermediate types through which to perform the conversion. For example, suppose a conversion exists from type Point to type Rect and a conversion exists from type int to type Point. The compiler does not supply a conversion from type int to type Rect by constructing an intermediate object of type Point.
0
 
nietodCommented:
Just to add to jkr's post a little bit.

A conversion constructor is a constructor that takes a single non-default parameter that is not of the type to be constructor (or a reference to the type to be constructued) and that is not declared as an explicit constuctor.  the compiler will use this constructor to convert items of the parameter type (the non-default parameter, that is) to the class type the constructor is for.   So for example,

class Point
{
public:
   Point(int i); // A constructor that is also a conversion constructor.
}

void F(point p)
{
}

int main()
{
    int i = 5;

    F(4); // the $ is converted to a Point, the F is called with this Point.

   Point p = i; // The int is converted to a Point, then the point is copy-constructed to p.
    return 0;
};
0
 
KeziaAuthor Commented:
In the conversion constructor declared by nietod, what does the body of the constructor have? Or is it sufficient if you just declare such a conversion constructor?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jkrCommented:
>>In the conversion constructor declared by nietod

In which way is that one different from mine? :o)

As all the above samples contain no information what a 'Point' actually is/does, we would have to extend the sample:

class Point
{
public:
   Point();
   Point( int i);
private:

   double the_point;
};

Point::Point( int i) {

  the_point = (double) i;
}
0
 
KeziaAuthor Commented:
Is it necessary, that the class Point defined in the samples, should have a default constructor?

0
 
jkrCommented:
>>Is it necessary, that the class Point defined in the
>>samples, should have a default constructor?

No, it isn't necessary.
0
 
nietodCommented:
>> In which way is that one different from mine? :o)
Its not.  Just adding a few little peices.  Like examples of construction and a definition of what a conversion constructor is.

>> what does the body of the constructor have? Or is
>> it sufficient if you just declare such a conversion constructor?
Like any other constructor, it must have a body (if you use it).   As jkr said, we left it out just because ts an ordinary constrctor.  There is nothing special about it except that it has only one non-default parameter.   (It can have more than 1 paameter, but the others must have defaults).

Note also that this feature can be prevented--which can be a good idea at times.  Sometimes you don't want the compiler converting your types for you as sometimes its a costly (slow) operation and sometimes it means you've made a programming mistake and passed the wrong type.   So if you prevent conversion, you will get a compiler error any time you try to do an opeation that would require the conversion constructor.  This error lets you find and fix the problem.

to prevent a constructor from being a conversion constructor, declare it as explicit, like

class Point
{
public:
   explict Point(int i); // Note a conversion constructor, still a constructor though.
}

void F(point p)
{
}

int main()
{
   int i = 5;

   F(4); // Error, no conversion possible.
   F(Point(4)); // This is okay.

    Point p = i; // Error, no conversion possible.
   Point p = Point(i); // This is okay.
   Point p(i); // This is also okay (and preferred).
   return 0;
};
0
 
nietodCommented:
Kezia, you're free to choose for the answer the post that you think helped you the most, but you should make sure you make that choice fairly and carefully.  I had just added 1 or 2 points that jkr had left out of his answer, it seemed--to me at least--like his post was the answer (and it was there first).     But the decission is yours...

But thanks.
0
 
jkrCommented:
Err, what??????????????
0
 
MoondancerCommented:
Kezia ->  Welcome to Experts-Exchange, I see that you are new to this site.  Here are some helpful hints about us and some of the processes used here.
http://www.experts-exchange.com/jsp/qShow.jsp?ta=new_users&qid=20308976

Perhaps it was your intention to split points to both experts, or perhaps just an oversight in awarding the second, I don't know the answer to this.  Again, the links above are helpful in these regards.

I processed a Points for question to jkr for his well-deserved A on this here:

Points for jkr -> http://www.experts-exchange.com/jsp/qShow.jsp?qid=20311347

Moondancer - EE Moderator
0
 
KeziaAuthor Commented:
jkr's posts did help me to a great extent in understanding conversion constructors. But, nietod's posts helped me in understanding the concept more clearly, since he had given a detailed example about the working of the conversion constructors. I was wondering if there was any way to split up the points between them; but couldn't find any such option. Thanks to you Moondancer for awarding points to jkr.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now