Pass An Object to A Function

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..

thank you
Mmmmm, you write your classes and functions, and then post the code.
the full code is too large to post, to be simple,
class B
{
public:
virtual int Exec();
};

class A : public B
{
public:
virtual int Exec();
private:
ST_INFO m_stInfo;
};

class C:
{
public:
int SetFilter(B *);
static C *instance() {return m_stC ? m_stC : new C() };
private:
C();
C(C &);
B b;
static C m_stC;
};

what confused me much is that how to implement C::SetFilter(B *) in the following condition:
void function()
{
A a;
B::instance()->SetFilter(a);
};
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:

1
2
3
4
5
6
7
8
9
class clonable {
   virtual clonable* clone() const = 0;
};

// Example:
class derived : public clonable {
   virtual derived* clone() const
      { return new derived( *this ); }   // Invokes copy constructor...
};


Now SetFilter() just needs to call clone() on its parameter.
thank you very much, it helps me a lot!!
Topic archived. No new replies allowed.