conversion operator and conversion ctor

S

subramanian100in

Consider the following program named x.cpp

#include <cstdlib>
#include <iostream>

using namespace std;

class First;

class Second
{
public:
Second();
Second(const First& arg);
int getValue(void) const;

private:
int val;
};

class First
{
public:
First(int arg = -1);
int getValue(void) const;
operator Second(void);

private:
int val;
};

Second::Second() : val(-1000)
{
cout << "Second::default ctor called. val = " << val << endl;
}

Second::Second(const First& arg) : val(arg.getValue())
{
cout << "Second:: one argument ctor called. val = " << val <<
endl;
}

inline int Second::getValue(void) const
{
return val;
}

inline First::First(int arg) : val(arg)
{
}

inline int First::getValue(void) const
{
return val;
}

inline First::eek:perator Second(void)
{
cout << "First::eek:perator Second() called" << endl;
return Second();
}

void print(const Second& obj)
{
cout << "From print(): " << obj.getValue() << endl;
return;
}

int main()
{
First obj(500);

print(obj);

return EXIT_SUCCESS;
}

This program compiles fine with g++3.4.3 as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
and produced the output
First::eek:perator Second() called
Second::default ctor called. val = -1000
From print(): -1000

I have the following question(for learning purpose only)
the print() function takes a 'Second' type object and I am passing a
'First' type object. From 'First' type to 'Second' type, there are two
possibilities namely,
First::eek:perator Second(void)
and
Second::Second(const First& arg)
Am I correct ? If so, then why doesn't the compiler give ambiguity
error for the two possibilities but instead picks up First::eek:perator
Second() ?

I am unable to understand this. Kindly clarify this. What does the
Standard say regarding which one among these two functions to be
chosen ?

Kindly explain.

Thanks
V.Subramanian
 
J

James Kanze

Consider the following program named x.cpp
#include <cstdlib>
#include <iostream>
using namespace std;
class First;
class Second
{
public:
Second();
Second(const First& arg);
int getValue(void) const;
private:
int val;
};
class First
{
public:
First(int arg = -1);
int getValue(void) const;
operator Second(void);

Shouldn't this function be const? It doesn't change the value
of First.
private:
int val;
};
Second::Second() : val(-1000)
{
cout << "Second::default ctor called. val = " << val << endl;
}
Second::Second(const First& arg) : val(arg.getValue())
{
cout << "Second:: one argument ctor called. val = " << val <<
endl;
}
inline int Second::getValue(void) const
{
return val;
}
inline First::First(int arg) : val(arg)
{
}
inline int First::getValue(void) const
{
return val;
}
inline First::eek:perator Second(void)
{
cout << "First::eek:perator Second() called" << endl;
return Second();
}
void print(const Second& obj)
{
cout << "From print(): " << obj.getValue() << endl;
return;
}
int main()
{
First obj(500);
print(obj);
return EXIT_SUCCESS;
}
This program compiles fine with g++3.4.3 as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
and produced the output
First::eek:perator Second() called
Second::default ctor called. val = -1000
From print(): -1000
I have the following question(for learning purpose only) the
print() function takes a 'Second' type object and I am passing
a 'First' type object.
From 'First' type to 'Second' type,
there are two possibilities namely,
First::eek:perator Second(void)
and
Second::Second(const First& arg)
Am I correct ?

The first only works if the expression to be converted is a
non-const.
If so, then why doesn't the compiler give ambiguity error for
the two possibilities but instead picks up First::eek:perator
Second() ?

For the same reasons the compiler generally prefers a non-const
function over a const--- in this case, First::eek:perator Second()
is an exact match, and Second::Second( First const& ) requires a
const conversion. In general, given a non-const lvalue, the
compiler will prefer a function which treats the object as a
non-const to one which treats it as const.
I am unable to understand this. Kindly clarify this. What does
the Standard say regarding which one among these two functions
to be chosen ?

That First::eek:perator Second() should be preferred for non-const
values, and Second::Second( First const& ) for const values.
 
F

Fraser Ross

The standard has unusual overload resolution rules for this and has the
term "ambiguous conversion sequence" for it. Have a look at
13.3.3.1/10.

Fraser.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top