Re: Need clarification regarding public functions from class CARD_LIST

al davis <[email protected]>
Newsgroups gmane.comp.gnu.gnucap.devel
Message-ID <[email protected]>
On Thursday 31 July 2014, al davis wrote:
> clone() makes a copy of itself.

more info ....

clone is always defined something like this:

class FOO : public SOME_BASE {
//////
//////
SOME_BASE* clone()const {return new FOO(*this);}
/////
};

I might use it like this:

int main()
{
  FOO aa;
  FOO* bb = aa.clone();
  // now I have two of them
}

Since it is just a wrapper for the copy constructor, why not 
just use the copy constructor directly?

Answer, because I really don't know exactly what type to copy.  
clone() is virtual.

So I might have:

int main()
{
   CARD* prototype = device_dispatcher["resistor"];
   CARD* an_instance = prototype.clone();
   CARD* another_instance  = prototype.clone();

   // or I could use the short form:
   CARD* yet_another = device_dispatcher.clone("capacitor");
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.