Re: templates - method return value is multiple types
This is a MIME GnuPG-signed message. If you see this text, it means that
your E-mail or Usenet software does not support MIME signed messages.
The Internet standard for MIME PGP messages, RFC 2015, was published in 1996.
To open this message correctly you will need to install E-mail or Usenet
software that supports modern Internet standards.
--=_mimegpg-monster.email-scan.com-10451-1306521428-0002
Content-Type: text/plain; format=flowed; delsp=yes; charset="UTF-8"
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
Stefan Ram writes:
Ruben Safir <mrbrklyn@panix.com> writes:
Stefan Ram <ram@zedat.fu-berlin.de> wrote:
Christopher <cpisz@austin.rr.com> writes:
Is it possible to create a method that can return different types
A function can return an instance of any type that is
a subtype of its declared return type (polymorphism).
what is a subtype? The template book i have makes it fairly clear that
changing the subtype, even with partial and explicit specialization, is
impossible because it changes the templates signature.
#include <cstdlib>
class A {}; class B: public A {}; class C: public A {};
A f(){ if( ::std::rand() )return B(); else return C(); }
int main(){}
Your function still returns an instance of A, in all cases. Yes, the
"return" statement can evaluate to any subclass.
However, what's evaluated by "return" is not the same thing as what the
function ultimately returns.
--=_mimegpg-monster.email-scan.com-10451-1306521428-0002
Content-Type: application/pgp-signature
Content-Transfer-Encoding: 7bit
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
iEYEABECAAYFAk3f71QACgkQx9p3GYHlUOIubQCeOizTphwxEI8Fu9i21PVWtD5g
LSsAniizfnfS3o7IFGb6tXokVccY33NI
=ouxE
-----END PGP SIGNATURE-----
--=_mimegpg-monster.email-scan.com-10451-1306521428-0002--