container to hold objects of class type and it's derived types

And was trying to have a vector that could hold any object of class A or it's derived types using pointers. I tried to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  class A 
  { };
   
  class B : A
  { 
     // memb variables and constructors
  };

  class C : B
  { 
     // memb variables and constructors
  };

//  In main function:

  vector<A*> items[50];
  C* x1 = new C(1, 2);
  items.push_back(&x1);



But that gives and error. Is there any way to do this sort of thing?

I also tried using random_shuffle on my A* vector and that gave an error as well, is there some problem using it with vectors of pointers?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <vector>
#include <memory>
#include <functional>

struct A { virtual ~A() = default ; };
struct B : A {};
struct C : A {};
struct D : C {};

int main()
{
    A a ; B b ; C c ; D d ;

    // fine: vector of non-owning raw pointers
    std::vector<A*> items { std::addressof(a), std::addressof(b),
                            std::addressof(c), std::addressof(d) } ;

    // fine: vector of wrapped references
    std::vector< std::reference_wrapper<A> > same_items { a, b, c, d } ;

    // fine: vector of owning smart pointers
    std::vector< std::unique_ptr<A> > more_items ;
    more_items.push_back( std::make_unique<A>() ) ;
    more_items.push_back( std::make_unique<B>() ) ;
    more_items.push_back( std::make_unique<C>() ) ;
    more_items.push_back( std::make_unique<D>() ) ;

    // **** bad ****: vector of raw owning pointers (avoid, would not pass a code review)
    std::vector<A*> even_more_items { new A, new B, new C, new D } ; // not exception safe
    for( auto ptr : even_more_items ) delete ptr ;
}
Last edited on
I tried do

1
2
C c1(1, 2);
vector< std::reference_wrapper<A> > same_items { c1 } ;


and now have the error

error: 'A' is an inaccessible base of 'C'|
Thank you so much. I'd been at this soo long...
> error: 'A' is an inaccessible base of 'C'

A is a private base class of C. What you wanted is something like this:
1
2
3
4
5
6
7
8
9
10
11
12
  class A 
  { virtual ~A() = default ;  }; // good idea to declare a virtual destructor
   
  class B : public A
  { 
     // memb variables and constructors
  }

  class C : public B
  { 
     // memb variables and constructors
  }
Ok... having one more issue....

Got it working when the vector was declared in main, but as soon as I tried to use the same vector as a member variable of a different class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Bag
{
    public:

    vector<std::reference_wrapper<A>> items[50]; 

    // other stuff
};

int main()
{
    Bag b;
    C c1(1, 2);

    b.items.push_back(c1);

    return 0;
}


I get "error: request for member 'A' in 'b', which is of non-class type 'Bag()'|"
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <vector>
#include <functional> // ****

class A  {  /* ... */ };

class B : public A { /* ... */ };

class C : public B
{
    public: C( int, int ) {  /* ... */  }

    // ...
};

class bag
{
    public:

    // vector<std::reference_wrapper<A>> items[50];
    std::vector< std::reference_wrapper<A> > items ; // ****
};

int main()
{
    bag b ;
    C c1( 1, 2 ) ;

    b.items.push_back(c1);
}

http://coliru.stacked-crooked.com/a/888af997af9de86e
Thank you.
1
2
3
4
5
vector<A*> items;
C c1(1, 2);
items.push_back(addressof(c1));

cout << items[0].memberVarAccesserFunct();

1
2
3
4
5
6
7
C c1;
vector<std::reference_wrapper<A*>> items;
items.push_back(c1);

C& x = dynamic_cast<C&>(items[0].get());

cout << x.memberVarAccesserFunct();


Ok so can now get all item types stored in the vector, now I' having trouble as to what to do with them after that...
If the base class A is a polymorphic type, we can invoke virtual functions on the objects and they would be dispatched at run time to the appropriate implementations.

For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <vector>
#include <memory>
#include <functional>
#include <iostream>

struct A
{
    virtual ~A() = default ;
    virtual void foo() const { std::cout << "A::foo\n" ; }
};

struct B : A
{
    virtual void foo() const override
    { std::cout << "B::foo (overrides A::foo)\n" ; }
};

struct C : A
{
    virtual void foo() const override
    { std::cout << "C::foo (overrides A::foo)\n" ; }
};

struct D : C
{
    virtual void foo() const override
    { std::cout << "D::foo (overrides C::foo)\n" ; }
};

int main()
{
    A a ; B b ; C c ; D d ;

    // fine: vector of non-owning raw pointers
    std::vector<A*> items { std::addressof(a), std::addressof(b),
                            std::addressof(c), std::addressof(d) } ;

    for( A* ptr : items ) if(ptr) ptr->foo() ;

    // fine: vector of wrapped references
    std::vector< std::reference_wrapper<A> > same_items { a, b, c, d } ;
    for( A& a : same_items ) a.foo() ;


    // fine: vector of owning smart pointers
    std::vector< std::unique_ptr<A> > more_items ;
    more_items.push_back( std::make_unique<A>() ) ;
    more_items.push_back( std::make_unique<B>() ) ;
    more_items.push_back( std::make_unique<C>() ) ;
    more_items.push_back( std::make_unique<D>() ) ;
    for( const auto& ptr : more_items ) if(ptr) ptr->foo() ;

    // **** bad ****: vector of raw owning pointers (avoid, would not pass a code review)
    std::vector<A*> even_more_items { new A, new B, new C, new D } ; // not exception safe
    for( A* ptr : even_more_items ) if(ptr) ptr->foo() ;
    for( auto ptr : even_more_items ) delete ptr ;
}

http://coliru.stacked-crooked.com/a/396d4633a77274a6
Topic archived. No new replies allowed.