hi all,
i got some problems when i implement my function.
i want to pass an object A to a function F of another object C.
there are two problems
1) A will be destroy soon after passed to F. so i think i have to pass the point and then have a copy in C. then comes the 2nd problem
2) A is derived from B, they have different variables, so how should i do to copy A to C? there may be A1, A2 that all derived from B and they have the different copy constructors..
You have to copy the object, but you have to be careful what you copy it into.
Copying an A into a B (where A is derived from B) is called splicing. When you do that, you are only copying the "B" portion of A... which is probably not what you want.
You will need C to store a pointer to B, and then SetFilter() needs to copy its argument. This will require B and its derived types to be CopyConstructible. You will then need to implement the clone pattern. The clone pattern is used to copy objects whose actual type you do not know. It is very simple: