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:perator Second(void)
{
cout << "First: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: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: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: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
#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:perator Second(void)
{
cout << "First: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: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: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: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