What pattern to use for the following scenario?

Adrian

I have the following class:

class Base {
public:
    Base(string name) {
        agg = new Aggregate(name);
    }
private:
    Aggregate* agg;
};

Now I need to extend this class:

class Derived : Base {
public:
    Derived(string name) : Base(name) {
        agg2 = new Aggregate2(name);
    }
private:
    Aggregate2* agg2;
};

What I want is when I create a Base object, Aggregate needs to be created and when I create a Derived object only Aggregate2 should be created.

Now this is not happening because Aggregate its created inside the constructor which is called when I create a Derived object and like this Aggregate and Aggregate2 would be created.

I could move the creation to a different method and call that after creating the object.
Is there any other elegant way to do what I want ?

Jarod42

You may use the following:

class Base {
public:
    explicit Base(string name) : agg(new Aggregate(name)) {}
protected:
    Base() = default;
private:
    std::unique_ptr<Aggregate> agg;
};

class Derived : Base {
public:
    // implicit call to Base(), you may be explicit if you want
    Derived(string name) : agg2(new Aggregate2(name)) {}
private:
    std::unique_ptr<Aggregate2> agg2;
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What pattern to use for the following scenario?

From Dev

How to use threading or Task pattern in following scenario

From Dev

How to use threading or Task pattern in following scenario

From Dev

Design pattern to handle following scenario

From Dev

What will happen in the following mulththreaded scenario?

From Dev

What Design Pattern can I use to accomplish the following

From Dev

How to use RethinkDB indices in the following scenario?

From Dev

How to use expandable list view in the following scenario

From Dev

What is best Ruby Class design / pattern for this scenario?

From Dev

What's the issue in hide/show in jQuery code for following scenario?

From Dev

What's the issue in accessing associative array element in smarty in following scenario?

From Dev

How to use create event on a jQuery dialog in following scenario?

From Dev

How to access json data and use in if condition in following scenario?

From Dev

How can I use list comprehension in following scenario?

From Dev

what pattern should I use to print the date,time and process id for the following code?

From Dev

What is optimal way to use branches with GIT in this scenario

From Dev

what aws services should i use in this scenario?

From Dev

What is a good design pattern for the following situation?

From Dev

Is the following scenario possible with Twilio

From Dev

SAS for the following scenario

From Dev

It is possible to use CSS to create the following border pattern?

From Dev

What is the use in & and && operator in the following program?

From Dev

What is the use in & and && operator in the following program?

From Dev

What SimpleDateFormat Pattern to use?

From Dev

What is the use of the negative pattern?

From Dev

How to manipulate the array in following scenario?

From Dev

How to change the array in following scenario?

From Dev

Does SSD obsolete following scenario

From Dev

How to get result in following scenario

Related Related

  1. 1

    What pattern to use for the following scenario?

  2. 2

    How to use threading or Task pattern in following scenario

  3. 3

    How to use threading or Task pattern in following scenario

  4. 4

    Design pattern to handle following scenario

  5. 5

    What will happen in the following mulththreaded scenario?

  6. 6

    What Design Pattern can I use to accomplish the following

  7. 7

    How to use RethinkDB indices in the following scenario?

  8. 8

    How to use expandable list view in the following scenario

  9. 9

    What is best Ruby Class design / pattern for this scenario?

  10. 10

    What's the issue in hide/show in jQuery code for following scenario?

  11. 11

    What's the issue in accessing associative array element in smarty in following scenario?

  12. 12

    How to use create event on a jQuery dialog in following scenario?

  13. 13

    How to access json data and use in if condition in following scenario?

  14. 14

    How can I use list comprehension in following scenario?

  15. 15

    what pattern should I use to print the date,time and process id for the following code?

  16. 16

    What is optimal way to use branches with GIT in this scenario

  17. 17

    what aws services should i use in this scenario?

  18. 18

    What is a good design pattern for the following situation?

  19. 19

    Is the following scenario possible with Twilio

  20. 20

    SAS for the following scenario

  21. 21

    It is possible to use CSS to create the following border pattern?

  22. 22

    What is the use in & and && operator in the following program?

  23. 23

    What is the use in & and && operator in the following program?

  24. 24

    What SimpleDateFormat Pattern to use?

  25. 25

    What is the use of the negative pattern?

  26. 26

    How to manipulate the array in following scenario?

  27. 27

    How to change the array in following scenario?

  28. 28

    Does SSD obsolete following scenario

  29. 29

    How to get result in following scenario

HotTag

Archive