T
thomas
#include<iostream>
#include<string>
using namespace std;
class Openit;
class OpenFile{
public:
OpenFile(const string& filename){
filename_ = filename;
}
OpenFile& readonly(){
readonly_ = true; return *this;
}
private:
friend class Openit;
string filename_;
bool readonly_;
};
class Openit{
public:
Openit(const OpenFile& p){ //(1)
cout<<"open"<<endl;
}
};
int main(){
Openit it = OpenFile("ok").readonly(); //(2)
Openit it(OpenFile("ok").readonly()); //(3)
return 0;
}
----------code--------------
Here we see that (2) and (3) are equivalent.
While (3) obeys the parameter format well, how can (2) work when
assigning a "OpenFile" object to "Openit" object?
#include<string>
using namespace std;
class Openit;
class OpenFile{
public:
OpenFile(const string& filename){
filename_ = filename;
}
OpenFile& readonly(){
readonly_ = true; return *this;
}
private:
friend class Openit;
string filename_;
bool readonly_;
};
class Openit{
public:
Openit(const OpenFile& p){ //(1)
cout<<"open"<<endl;
}
};
int main(){
Openit it = OpenFile("ok").readonly(); //(2)
Openit it(OpenFile("ok").readonly()); //(3)
return 0;
}
----------code--------------
Here we see that (2) and (3) are equivalent.
While (3) obeys the parameter format well, how can (2) work when
assigning a "OpenFile" object to "Openit" object?