From -3985678375825809674
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,18c6d861cef54158
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2003-01-01 18:26:31 PST
Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!kibo.news.demon.net!mutlu.news.demon.net!demon!mail2news.demon.co.uk!devnull
From: kon@iki.fi (Kalle Olavi Niemitalo)
Newsgroups: comp.std.c++
Subject: Re: automatic downcast support through function parameters
Date: Thu, 2 Jan 2003 02:26:24 +0000 (UTC)
Organization: Oulun Puhelin Oyj - Baana
Lines: 60
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <871y3xxi8m.fsf@Astalo.y2000.kon.iki.fi>
References: <23478c42.0212310830.23dbc580@posting.google.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7BIT
X-Trace: mail2news.demon.co.uk 1041474384 8496 10.0.0.1 (2 Jan 2003 02:26:24 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Thu, 2 Jan 2003 02:26:24 +0000 (UTC)
X-Received: from mulga.cs.mu.oz.au ([128.250.1.22])
	by news.demon.co.uk with esmtp (Exim 4.05)
	id 18Tv3u-0002Cs-00
	for mail2news@news.news.demon.net; Thu, 02 Jan 2003 02:26:22 +0000
X-Received: from localhost (localhost [[UNIX: localhost]]) by mulga.cs.mu.OZ.AU
	id NAA00337; Thu, 2 Jan 2003 13:26:13 +1100 (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++@ncar.ucar.edu
X-NNTP-posting-date: Wed, 1 Jan 2003 11:06:03 +0000 (UTC)
X-Accept-Language: fi;q=1.0, en;q=0.9, sv;q=0.5, de;q=0.1
X-User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50
X-Newsgroups: comp.std.c++
X-Keywords: dynamic_cast,upcasted
X-Spam-Status: No, hits=-7.2 required=5.0
	tests=EMAIL_ATTRIBUTION,NOSPAM_INC,REFERENCES,SPAM_PHRASE_00_01,
	      USER_AGENT,X_ACCEPT_LANG
	version=2.41
Xref: archiver1.google.com comp.std.c++:16559

danielgutson@hotmail.com (danielgutson@hotmail.com) writes:

>  void f(upcasted Base* b)    // see below
>  {
>    g(b);
>  }

In current C++, there are at least two ways to define this
function.

If you want the choice of g() to be made at compile time, based
on the static type of the argument given to f, then you can use
a template:

  template <typename T>
  void f(T *b)
  {
    /* TODO: Put something here to ensure that
       T is derived from Base.  */
    g(b);
  }

If the choice must be made at run time, based on the type of the
most derived object, then you can use RTTI:

  void f(Base *b)
  {
    if (Der *d = dynamic_cast<Der *>(b))
      g(d);
    else
      g(b);
  }

However, RTTI requires polymorphic types; Base would then need at
least one virtual function member.

These two methods differ in the following way:

  int main()
  {
    Der d;
    Base *b = &b;
    f(b);
  }

With the template, this calls f<Base>(Base *) which in turn calls
g(Base *); with the RTTI check, this calls f(Base *) which in
turn calls g(Der *).

Would your "upcasted" syntax be semantically equivalent to either
of these methods?

What happens if the parameter is a null pointer?

---
[ 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                       ]



