[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Gnucap-devel] Need clarification regarding public functions from cl
From: |
al davis |
Subject: |
Re: [Gnucap-devel] Need clarification regarding public functions from class CARD_LIST |
Date: |
Thu, 31 Jul 2014 23:23:33 -0400 |
User-agent: |
KMail/1.13.7 (Linux/3.2.0-4-amd64; KDE/4.8.4; x86_64; ; ) |
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");
}