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
Solved

Conversion constructors in C++

Posted on 2002-06-12
11
410 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dialogbox API leak? 18 98
How can i compile this github project?? 2 90
c++, dynamic object by json 1 53
Copy output image from TWindowsMediaPlayer 6 48
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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

840 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