From 1985250188449795185
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,a1bad1ffc4f79f48
X-Google-Attributes: gidf78e5,public
Path: g2news1.google.com!news2.google.com!proxad.net!newsfeed.icl.net!kibo.news.demon.net!mutlu.news.demon.net!demon!mail2news.demon.co.uk!devnull
From: richard@ex-parrot.com (Richard Smith)
Newsgroups: comp.std.c++
Subject: Re: Idea for calling multiple methods on a single object.
Date: Mon, 7 Jun 2004 16:47:14 +0000 (UTC)
Organization: http://groups.google.com
Lines: 66
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <1a0929fa.0406070510.de6c30d@posting.google.com>
References: <194c1211.0406060903.6731d4ee@posting.google.com>
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: mail2news.demon.co.uk 1086626834 22226 10.0.0.1 (7 Jun 2004 16:47:14 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Mon, 7 Jun 2004 16:47:14 +0000 (UTC)
X-Received: from mulga.cs.mu.oz.au ([128.250.1.22])
	by news.demon.co.uk with esmtp (Exim 4.12)
	id 1BXNHF-0005mL-00
	for mail2news@news.news.demon.net; Mon, 07 Jun 2004 16:47:14 +0000
X-Received: from mulga.cs.mu.OZ.AU (localhost [127.0.0.1]) by mulga.cs.mu.OZ.AU with ESMTP
	id i57GlBB5020758; Tue, 8 Jun 2004 02:47:11 +1000 (EST)
X-Received: (from fjh@localhost)
	by mulga.cs.mu.OZ.AU (8.12.10+Sun/8.12.9/Submit) id i57GlBJ3020749;
	Tue, 8 Jun 2004 02:47:11 +1000 (EST)
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Path: comp-std-cpp-robomod!not-for-mail
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Delivered-To: std-c++@ucar.edu
X-Newsgroups: comp.std.c++
X-NNTP-Posting-Date: Mon, 7 Jun 2004 13:10:55 +0000 (UTC)
X-Spamscanner: mailbox4.ucsd.edu  (v1.4 May 20 2004 13:55:33, 0.0/5.0 2.63)
X-MailScanner: PASSED (v1.2.8 60069 i57DAwTa083696 mailbox4.ucsd.edu)
X-Spam-Checker-Version: SpamAssassin 2.60-mulga_r1 (1.212-2003-09-23-exp) on 
	mulga.cs.mu.OZ.AU
X-Spam-Level: 
X-Spam-Status: No, hits=-3.8 required=5.2 tests=AWL,BAYES_00 autolearn=ham 
	version=2.60-mulga_r1
Xref: g2news1.google.com comp.std.c++:684

bekkah@web.de (Helium) wrote in message news:<194c1211.0406060903.6731d4ee@posting.google.com>...
> Multiple method-call:
  [...]
> Another possible solution is to store the rusult somewhere.
> 
> {
>    Type tmp = foo ();
>    tmp.method1 ();
>    tmp.method2 ();
> }
> 
> 
> This is very unconvenient and the result has to be copied which cvan
> be time consuming as well.

So use a reference:

  Type const& tmp = foo();
  tmp.method1();
  tmp.method2();

> And you have to know the function's return
> type. 

This is true.  Hopefully the next version of the Standard will allow
this to be solved using the auto keyword:

  auto const& tmp = foo(); // Or whatever the syntax is going to be.
  tmp.method1();
  tmp.method2();

>If it's a reference to an object its type isn't know at
> compile-time, 

You mean the type needn't be complete: it is still known.

>so you have to use a reference, too. If it's a temporary
> object you can't store it in a reference but you have to make a copy
> of it. 

You can bind a temporary to a reference which extends the lifetime of
the temporary.  Suppose foo() returns a Type by value, I can still
write

  Type const& tmp = foo();

and the lifetime of the temporary is extended for the lifetime of the
reference. (The compiler is allowed to make a copy of the return
value, but a good compiler will not.  -- See 8.5.3/5, bullet 2,
sub-bullet 1.)

> In both cases you have to create an additional temporary
> (weither a reference or a complete object).

Creating a reference is very cheap.  It may well not involve any
additional runtime overhead.

--
Richard Smith

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]



