Solved

Conversion constructors in C++

Posted on 2002-06-12
11
398 Views
Last Modified: 2011-08-18
What are conversion constructors and why are they called so?
0
Comment
Question by:Kezia
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>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
Comment Utility
Is it necessary, that the class Point defined in the samples, should have a default constructor?

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
Err, what??????????????
0
 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now