comp.lang.ada
 help / color / mirror / Atom feed
From: Martin Krischik <martin@krischik.com>
Subject: Re: how can i allocate an objekt with initialization???
Date: Mon, 06 Dec 2004 14:34:13 +0100
Date: 2004-12-06T14:34:13+01:00	[thread overview]
Message-ID: <1654535.Ng2VDZFboh@linux1.krischik.com> (raw)
In-Reply-To: 41b44663$0$9298$ba620e4c@news.skynet.be

Adrien Plisson wrote:

> Ole-Hjalmar Kristensen wrote:
> the corresponding C++ example should have been:
> 
> int main(int argc, char *argv[])
> {
>    a &my_object = b(1);

I have seen this before and I am wondering if it is actualy valid. Because:
where is the result of the b(1) constructor call been stored? a& can only
hold a reference to retun value. Is this not a variation on the classic
mistake.

int&
f ()
  {
  auto int retval = 5;

  // do something

  return retval;
  }

and that would mean that the livetime of the constructor call result is
undefined.

>    my_object.f();
> 
>    return 0;
> }
> 

Martin

PS: The above problem case can actually happen to anyone - when hidden by
implicid type convertion

extern int &f(); -- returns valid reference

unsigend&
g ()
  {
  return f();  -- just delegate.
  }

IBM C++ will convert int& to unsigned& - works fine
MS C++ will convert int& to int to unsigned to unsigned& - :-((

-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com



  reply	other threads:[~2004-12-06 13:34 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-05 16:54 how can i allocate an objekt with initialization??? Thomas Bruns
2004-12-05 22:51 ` Stephen Leake
2004-12-06  8:52 ` Martin Krischik
2004-12-06 10:04   ` Ole-Hjalmar Kristensen
2004-12-06 11:49     ` Adrien Plisson
2004-12-06 13:34       ` Martin Krischik [this message]
2004-12-13  6:38         ` Dave Thompson
2004-12-13 11:11           ` Martin Krischik
2004-12-06 11:55     ` Martin Krischik
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox