CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2003
    Posts
    30

    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

  2. #2
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    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

  3. #3
    Join Date
    Dec 2003
    Posts
    30
    now I got it. If one could only know when temporary objects are
    created by the system?

  4. #4
    Join Date
    Sep 2002
    Posts
    1,747
    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

  5. #5
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    Temporary objects are also created by functions which return an object by value.
    All the buzzt
    CornedBee

  6. #6
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    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.

  7. #7
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    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.

  8. #8
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    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
  •  





Click Here to Expand Forum to Full Width

Featured