Solved

Conversion constructors in C++

Posted on 2002-06-12
11
412 Views
Last Modified: 2011-08-18
What are conversion constructors and why are they called so?
0
Comment
Question by:Kezia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7072678
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
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 7073046
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
 

Author Comment

by:Kezia
ID: 7073219
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!

 
LVL 86

Expert Comment

by:jkr
ID: 7073235
>>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
 

Author Comment

by:Kezia
ID: 7073267
Is it necessary, that the class Point defined in the samples, should have a default constructor?

0
 
LVL 86

Expert Comment

by:jkr
ID: 7073275
>>Is it necessary, that the class Point defined in the
>>samples, should have a default constructor?

No, it isn't necessary.
0
 
LVL 22

Expert Comment

by:nietod
ID: 7073345
>> 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
 
LVL 22

Expert Comment

by:nietod
ID: 7075102
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
 
LVL 86

Expert Comment

by:jkr
ID: 7075289
Err, what??????????????
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7076213
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
 

Author Comment

by:Kezia
ID: 7080366
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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question