Andrew said:
$ cat < test.cpp
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "d"; }
A(const A& a) : x(a.x) { cout << "c"; }
int getx() const { return x; }
int x;
};
class B
{
public:
B(int i) : x(i) {}
operator A() { A a; a.x = x; return a; }
int x;
};
void f(int x) { cout << x << endl; }
int main(int argc, char** argv)
{
B b(argc);
f(A(b).getx());
}
$ g++ test.cpp
$ ./a.out
d1
I only count one d.
Due to the return value optimization...
Imagine how much less embarrassed you would be if you stated it this
way:
"I think that two temporaries will be created, won't they?"
and left it at that.
Enjoy,
Andrew.
Cause for my embarrassment is wholly imaginary.
The following produced:
[[
Boo:

perator ZooString()const
ZooString<0013FF48>::ZooString(const char *) {Boo}
ZooString<0013FF2C>::ZooString(const ZooString &) {Boo}
Boo
]]
Note the copy construction.
//================================
//================================
#include <iostream>
#include <string>
//================================
//================================
class ZooString
{
public:
ZooString(const char * s = "")
: str(s)
{ PrintTo(std::cout, "::ZooString(const char *)") << '\n';
}
ZooString(const ZooString & r)
: str(r.str)
{ PrintTo(std::cout, "::ZooString(const ZooString &)") << '\n';
}
const char * c_str() const
{
return str.c_str();
}
std:

stream & PrintTo(std:

stream & os, const char * msg) const
{
os << "ZooString<" << (void*)this << '>' << msg
<< " {" << str << '}';
return os;
}
private:
std::string str;
};
//================================
//================================
class Boo
{
public:
Boo(){}
Boo(const Boo &);
operator ZooString() const
{ std::cout << "Boo:

perator ZooString()const\n";
return "Boo";
}
};
//================================
//================================
int main(int, char**)
{
Boo hoo;
std::cout << ZooString(hoo).c_str() << '\n';
return 0;
}