0

Will someone please point me in the correct directions. I want a "static" (singular instance) std::list, so the std::list is shared within all objects having an instance of the "Container" structure seen below. However, all other elements within the structure are to remain unique to each instance.

Where am i to place the keyword "static" for the derived std::list?

Note the keyword "static" below is misplaced, as i simply don't know where it should be in this case.

struct Container : public static std::list<int>
{

public:

    Container()
    {}

    ~Container()
    {}

    list<int*> Handles;

}; // struct
8-bitButterfly
  • 203
  • 2
  • 13

4 Answers4

7

You don't need inheritance here; the best way to do it is to have a static member:

struct Container {
    static std::list<int> list_;
}

There is no static inheritance as well, so I would propose to implement your own get, insert, remove methods that would modify internal static list.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
nogard
  • 9,432
  • 6
  • 33
  • 53
  • Agree this is the right way to do it, but strictly speaking should this be called composition? – BoBTFish May 10 '13 at 09:05
  • I wasn't complaining it was wrong. It was a purely academic question that I didn't know the answer to. – BoBTFish May 10 '13 at 09:09
  • @BoBTFish Yeah, but your intent was right: it's not composition, because composition means that your instances contain other stuff ("has-a"). But if all instances "have" the same stuff, you can't say an instance "has" this stuff, since it doesn't belong to one specific instance (because it's static...) Think of composition as "has-a" with non-shared ownership... – leemes May 10 '13 at 09:12
  • It's a `shares-a` relationship! – Lightness Races in Orbit May 10 '13 at 09:19
  • Wow! Thank you for the quick response. I have attempted earlier the method in your suggestion. However, i don't know how to ::iterate the list within the structure from within one of the instances. Will you please show me how? I need to do: Container myContainer; list::iterator niContainer(myContainer.list_.begin()); – 8-bitButterfly May 10 '13 at 09:20
  • @8-bitButterfly: In order to iterate the easiest you can do is just create method returning const reference to the static member: `const std::list & getList() const;` And then iterate it easily. – nogard May 10 '13 at 09:23
  • 1
    @8-bitButterfly: Comments are for requesting clarification and bashing newbs, not for "follow-up questions". – Lightness Races in Orbit May 10 '13 at 09:26
1

You should not inherit from std::list; instead it seems like you want a single static member instead:

class Container
{
    static std::list<int> SingleList;

    // ...
}

By making a member variable static there is only one single instance of that variable, shared between all instance of the class it's declared in. Just like you want.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thank you. As i posted above. I have attempted earlier the method in your suggestion. However, i don't know how to ::iterate the list within the structure from within one of the instances. Will you please show me how? I need to do: Container myContainer; list::iterator niContainer(myContainer.list_.begin()); – 8-bitButterfly May 10 '13 at 09:26
  • @8-bitButterfly: Comments are for requesting clarification and bashing newbs, not for "follow-up questions". – Lightness Races in Orbit May 10 '13 at 09:27
  • 1
    @8-bitButterfly: Okay but I suggest actually spending some time now _trying to do it yourself_ first. Come back in a few days if you still can't work it out. – Lightness Races in Orbit May 10 '13 at 09:32
0

I believe a static member works for your case.

struct Container
{
public:
    Container()
    {}

    ~Container()
    {}

public:
    static std::list<int*> Handles;
}; // struct
Bingfeng
  • 147
  • 1
  • 9
0

As others have said, inheriting from standard containers is bad, and the reason this is bad is because standard containers have no virtual destructor defined. This means that upon destruction, you will not be destroying the derived object properly, when trying to destroy it through a pointer to base.

Base* b = new derived();

// do stuff

delete b; // will not destroy derived properly because base has no virtual destructor.
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
  • 2
    _Actually_, it means that upon destruction through a reference or pointer to parent, [you will be invoking **undefined behaviour** (`[C++11: 5.3.5/3]`)](http://stackoverflow.com/questions/2065938/virtual-destructor-is-it-required-when-not-dynamically-allocated-memory). – Lightness Races in Orbit May 10 '13 at 09:23
  • @Angew: Derived _is_ child. Tony used "base" and "child"; you used "parent" and "derived"; both of you in fact used the terms correctly, but why not be symmetrical and stick to "base/derived"? – Lightness Races in Orbit May 10 '13 at 09:24
  • @LightnessRacesinOrbit See edit history - my comment came before the first edit. I will remove it, it's superfluous now. – Angew is no longer proud of SO May 10 '13 at 09:34