S
Steven T. Hatton
What should I expect the following code to print? Is it defined in the
Standard? What does it produce for you? I was kind of surprised by what
GCC 4.0.2 made of it.
#include <string>
#include <iostream>
typedef std::string object;
class arrow{
public:
arrow(object& domain_
,object& codomain_
,const std::string& name_)
:_domain(domain_)
,_codomain(codomain_)
,_name(name_)
{
std::cout << _name << std::endl;
}
object domain(){ return _domain; }
object codomain(){ return _codomain; }
operator object () { return _codomain; }
std:stream& print(std:stream& out) const {
return out<<_domain<<"--"<<_name<<"->"<<_codomain;
}
private:
object _domain;
object _codomain;
const std::string& _name;
};
std:stream& operator<<(std:stream& out, const arrow& a ){
return a.print(out);
}
int main(){
object a("a");
object b("b");
arrow f(a,b,"f");
std::cout << f << std::endl;
}
Standard? What does it produce for you? I was kind of surprised by what
GCC 4.0.2 made of it.
#include <string>
#include <iostream>
typedef std::string object;
class arrow{
public:
arrow(object& domain_
,object& codomain_
,const std::string& name_)
:_domain(domain_)
,_codomain(codomain_)
,_name(name_)
{
std::cout << _name << std::endl;
}
object domain(){ return _domain; }
object codomain(){ return _codomain; }
operator object () { return _codomain; }
std:stream& print(std:stream& out) const {
return out<<_domain<<"--"<<_name<<"->"<<_codomain;
}
private:
object _domain;
object _codomain;
const std::string& _name;
};
std:stream& operator<<(std:stream& out, const arrow& a ){
return a.print(out);
}
int main(){
object a("a");
object b("b");
arrow f(a,b,"f");
std::cout << f << std::endl;
}