Parameter passing and assignment

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?
 
J

James Kanze

(e-mail address removed):
readonly_ not initialized and has a random value!
Also prefer initialization over assignment:
OpenFile(const string& filename)
: filename_(filename), readonly_(false) {}

Maybe. They're not equivalent to the compiler.

(2) doesn't do any assignment. It's copy initialization. And
both the copy constructor and the assignment operator are
defined for the class.
Formally, by using the compiler-generated copy constructor
Openit(const Openit&). However, the copy constructor is
allowed to be skipped (optimized away) in certain cases (as
here), so (2) and (3) are often strictly equivalent in
practice.

Yes and no. If he adds a member of type std::fstream to
OpenFile, then they won't be; the compiler won't generate a copy
constructor if any of the members isn't copiable. And while the
compiler is allowed to skip the actual copy, it's not allowed to
skip verifying that the copy is legal.
Note that in both cases OpenFile object is a temporary, which
dies at the end of full expression (semicolon here). As it is
a temporary, it cannot represent an "open file" quite well in
my mind, so the name is a bit misleading IMO.

He's obviously simplified, and removed significant parts of the
class; if it's called OpenFile, it definitely needs either an
std::fstream or a std::filebuf member.

He also needs to learn the difference between initialization and
assignment, the two types of initialization and how the compiler
handles copy construction and assignment. All things that
should be covered in any good basic text on the language.
 
T

thomas

Formally, by using the compiler-generated copy constructor Openit(const
Openit&). However, the copy constructor is allowed to be skipped
(optimized away) in certain cases (as here), so (2) and (3) are often
strictly equivalent in practice.

In which case can the copy constructor be skipped? Any reference?
 
T

thomas

In which case can the copy constructor be skipped? Any reference?

Sorry but I still cannot get it with regard to assigning a class to
another.
Although the difference between initialization list and assignment is
discussed widely, I cannot find any reference more related.
 
J

James Kanze

Sorry but I still cannot get it with regard to assigning a
class to another. Although the difference between
initialization list and assignment is discussed widely, I
cannot find any reference more related.

The important thing to realize is that in C++, the '=' token
(like ',') can be either an operator or other punctuation. When
it's an operator, the assignment operator (overloaded or
otherwise) is invoked. When it's not an operator, however, the
assignment operator has nothing to do with it, and is not used.
In a declaration, the '=' between the variable being declared
and the initialization expression is NOT an operator. It's
simply part of the syntax of the definition. And no assignment
is present in something like:
MyType a = b;
This is called copy initialization, and formally consists of b
being converted to type MyType, resulting in a temporary, and
the copy constructor being invoked. The compiler has the right
to suppress the temporary (and the copy which ensues), but only
if the code would be legal if it didn't.

In general, if you need a copy constructor, you'll need an
assignment operator, and vice versa.
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top