Solved

Conversion constructors in C++

Posted on 2002-06-12
11
403 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SendMessage fails while PostMessage works 2 107
Lambda for random numbers problem 7 107
Should CArray be used for a list of pointers in C++? 19 97
White board coding practice 3 80
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

895 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

14 Experts available now in Live!

Get 1:1 Help Now