From 3682642228926474863
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,18c6d861cef54158
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2003-01-02 11:23:11 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: philippe_mori@hotmail.com ("Philippe Mori")
Newsgroups: comp.std.c++
Subject: Re: automatic downcast support through function parameters
Date: Thu, 2 Jan 2003 19:23:07 +0000 (UTC)
Organization: Bell Sympatico
Lines: 49
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <rIZQ9.5172$8M3.858318@news20.bellglobal.com>
References: <23478c42.0212310830.23dbc580@posting.google.com>
X-Trace: mail2news.demon.co.uk 1041535387 16348 10.0.0.1 (2 Jan 2003 19:23:07 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Thu, 2 Jan 2003 19:23:07 +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 18UAvq-0004FX-00
	for mail2news@news.news.demon.net; Thu, 02 Jan 2003 19:23:06 +0000
X-Received: from localhost (localhost [[UNIX: localhost]]) by mulga.cs.mu.OZ.AU
	id GAA04820; Fri, 3 Jan 2003 06:22:57 +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-Newsgroups: comp.std.c++
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4133.2400
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400
X-NNTP-Posting-Date: Thu, 02 Jan 2003 11:23:51 EST
X-Spam-Status: No, hits=-0.4 required=5.0
	tests=FORGED_HOTMAIL_RCVD,INVALID_MSGID,NOSPAM_INC,
	      PRIORITY_NO_NAME,REFERENCES,SPAM_PHRASE_05_08
	version=2.41
Xref: archiver1.google.com comp.std.c++:16649


> automatic downcast during function calls.
> let's have:
>  struct Base{};
>  struct Der : Base{};
>
>  void g(Base* b);
>  void g(Der* d);
>
>  void f(upcasted Base* b)    // see below
>  {
>    g(b);
>  }
>
>  [...]
>
> Daniel.
>

In practice, I think that the compiler would have to knows every derived
classes to make it works and this is not really feasible particulary if some
module are dynamically linked.

The solution to your problem is either to add a virtual function to you base
class and call it (you may also uses design pattern like the "Visitor"):

    struct Base { virtual void do_g(); };
    struct Derived : Base { virtual void do_g(); };

    void f(Base *b) {
        b->do_g();
    }

>From that, some imagination  and possibly also with generic (template) code
(see Kalle post), you would be able to do what you want... If you do not
want to mess up the base class with a lot of stuff, you may prefer to have
one "creator" function that will create another object (derived from another
base class) on which you would call those functions...

One problem with template (only) based solution it that they will work on
static type. For example, it won't apply to a container of polymorphic
pointers.

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



