Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11598

Re: namespace and global function

From "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Newsgroups comp.lang.c++
Subject Re: namespace and global function
Date 2011-11-18 13:17 +0100
Organization A noiseless patient Spider
Message-ID <ja5idf$9rl$1@dont-email.me> (permalink)
References <1997973f-a426-4f32-9a58-6195c424f1d3@hh9g2000vbb.googlegroups.com>

Show all headers | View raw


On 17.11.2011 23:09, Jayden Shui wrote:
>
> I have a global function to clone an object.
>
> template<class T>
> T* Clone(T const&  t)
> {
>      return new T(t);
> }

This will use the statically known type, so if T is polymorphic it may 
not necessarily clone in the conventional meaning of the word as 
creating a copy.

Rather, when the dynamic type of `t` is a subclass of T, you'll get a slice.

That's why 'clone' is usually a virtual member function.


Cheers & hth.,

- Alf

Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

namespace and global function Jayden Shui <jayden.shui@gmail.com> - 2011-11-17 14:09 -0800
  Re: namespace and global function Joshua Maurice <joshuamaurice@gmail.com> - 2011-11-17 15:29 -0800
  Re: namespace and global function SG <s.gesemann@gmail.com> - 2011-11-18 01:00 -0800
  Re: namespace and global function "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-11-18 13:17 +0100

csiph-web