How to call back?

Discussion in 'C++' started by thomas, May 12, 2011.

  1. thomas

    thomas Guest

    Hi guys,

    ------------------------------------------------------------------
    struct Item{
    int i;
    };

    class Child(){
    void dosth(){
    for (int i = 0; i < 100; ++i)
    {
    Item* newItem = new Item();
    Parent.push(newItem); //A
    }
    }
    }

    class Parent{
    public:
    void push(Item* p){
    VecItem.push_back(p);
    }
    vector<Item*> VecItem;
    Child* pChild;
    };
    -----------------------------------------------------------------

    How to modify line A to make it work correctly and elegantly?
    1. I don't want to pass vectors between classes
    2. I infer if something like allocator can make it work.
    3. I don't want to expose the whole class Parent to Child, because
    there are many other interfaces that Child doesn't need.
    thomas, May 12, 2011
    #1
    1. Advertising

  2. thomas wrote, On 12.5.2011 4:43:
    > Hi guys,
    >
    > ------------------------------------------------------------------
    > struct Item{
    > int i;
    > };
    >
    > class Child(){
    > void dosth(){
    > for (int i = 0; i < 100; ++i)
    > {
    > Item* newItem = new Item();
    > Parent.push(newItem); //A
    > }
    > }
    > }
    >
    > class Parent{
    > public:
    > void push(Item* p){
    > VecItem.push_back(p);
    > }
    > vector<Item*> VecItem;
    > Child* pChild;
    > };
    > -----------------------------------------------------------------

    Please try to make a "working" example. Yours contains several syntactic
    errors. It is not clear what is the intention of the code and what is the
    real error.

    >
    > How to modify line A to make it work correctly and elegantly?
    > 1. I don't want to pass vectors between classes
    > 2. I infer if something like allocator can make it work.

    I do not understand this.

    > 3. I don't want to expose the whole class Parent to Child, because
    > there are many other interfaces that Child doesn't need.

    Try using pure virtual functions and "interface" classes to expose only the
    necessary interface of Parent to Child.

    --
    VH
    Vaclav Haisman, May 12, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joby
    Replies:
    0
    Views:
    1,627
  2. sylvia sil
    Replies:
    1
    Views:
    614
    Curt_C [MVP]
    Dec 29, 2004
  3. =?Utf-8?B?Z29yaWxsYQ==?=
    Replies:
    1
    Views:
    763
    Masudur
    May 25, 2007
  4. Author
    Replies:
    6
    Views:
    3,084
    George
    Jan 22, 2009
  5. martin

    Reading a file twice, back to back ?

    martin, Apr 16, 2006, in forum: Perl Misc
    Replies:
    16
    Views:
    158
Loading...

Share This Page