|
-
January 2nd, 2004, 01:38 PM
#1
could you translate this code in english
Hi,
I was reading Thinking in C++ by Bruce Eckel and I got stuck with this example:
Code:
class X {
int i;
public:
X(int ii = 0);
void modify();
};
X::X(int ii) { i = ii; }
void X::modify() { i++; }
X f5() {
return X();
}
const X f6() {
return X();
}
void f7(X& x) { // Pass by non-const reference
x.modify();
}
int main() {
f5() = X(1); // OK -- non-const return value
f5().modify(); // OK
//! f7(f5()); // Causes warning or error
// Causes compile-time errors:
//! f7(f5());
//! f6() = X(1);
//! f6().modify();
//! f7(f6());
} ///:~
Ok, the line: f5() = X(1), does it say that assign X(1) to the return value of function f5() which is of a type X?
And the: X(1), shouldn`t one need to say: X myXclass(1)??
Why is f7(f5()) illegal? They are both non-const.
-Thanks
-
January 2nd, 2004, 03:36 PM
#2
Ok, the line: f5() = X(1), does it say that assign X(1) to the return value of function f5() which is of a type X?
Yes.
And the: X(1), shouldn`t one need to say: X myXclass(1)??
No, X(1) is an unnamed temporary object.
Why is f7(f5()) illegal? They are both non-const.
The return value of f5 is a temporary object. You can't assign a temporary object to a non-const reference.
All the buzzt
CornedBee
-
January 2nd, 2004, 05:05 PM
#3
now I got it. If one could only know when temporary objects are
created by the system?
-
January 2nd, 2004, 07:29 PM
#4
A temporary object is also an anonymous object. When you see an object of some type created without an instance name attached to it, it is anonymous and temporary. The example of X(1) is a perfect example, as it creates an anonymous object of type X built from the X::X(int) construction method. Anonymous instances have some different rules attached to them from other automatics. In particular, they cannot be lookup matched to a non-const reference, and unless explicitly assigned to a reference, their scope is only until the end of the current expression.
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
-
January 3rd, 2004, 06:10 AM
#5
Temporary objects are also created by functions which return an object by value.
All the buzzt
CornedBee
-
January 5th, 2004, 05:08 PM
#6
Originally posted by CornedBee
The return value of f5 is a temporary object. You can't assign a temporary object to a non-const reference.
Interestingly, this compiles fine with VC6 and VC.NET when you enable language extensions. Apart from the fact that is illegal under ISO, I can't see a reason why it should be illegal. Or are there potential pitfalls ?
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
-
January 5th, 2004, 08:32 PM
#7
I can't remember exactly the reason and where I read it, but it has something to do with changing of value to temporary object in a chain of operations in a statement, like multiple assignment or << operations, that leads to some unexpected behavior.
-
January 6th, 2004, 02:38 PM
#8
Might also have to do something with non-const references to be expected to persist? I don't know...
All the buzzt
CornedBee
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|