storing derived abstract classes

Started by
16 comments, last by ekrax 19 years, 8 months ago
ok i forgot that i can't create an instance of an abstract class that contains pure virtual functions ... so now how do i create an instance to get a refrence if its not a template function?
Advertisement
Quote:Original post by ekrax
that would make more sense to do it that way, but i cant get it to work.

void add (base *object)
{
base *p = new base;
*p = object;
refrences.push_back (&p);
}


One you can't instantiate abstract classes only concreate sub-types. Also you don't need to take the addresss of the pointer to assign to a pointer, and if your actually gonna instantate with-in that function then this is where you need the virtual constructor idiom.

Quote:Original post by ekrax
doesnt work if i use something like ...
stuff.add (d1 (9));


That is asking for trouble unless the parameter was a constant reference to type base.
ok i got ya, now though looking back at the template version im confused about this ...

stuff.add <d1> (9);

however the add function is like this ...

template <class type> void add (type object);

however im passing an integer to the function add but yet it still works somehow, then i changed the function call to this ...

stuff.add <d1> (d1 (9));

so now im actually sending a class type to it not just an integer, but then this line somehow works ...

type *p = new type (object) ...

isnt this using the entire object as the constructor? the constructor expects an int and this sends itself as an object as the constructor.

jesus this is magic code!
Quote:Original post by ekrax
ok i forgot that i can't create an instance of an abstract class that contains pure virtual functions ... so now how do i create an instance to get a refrence if its not a template function?


Okay with your code from above and using virtual constructor idiom to achieve what was trying to achieve:

#include <vector>#include <iostream>struct base {   virtual base* clone() const = 0;   virtual base* create() const = 0;   virtual void show () const {      std::cout << "base\n";   }   virtual ~base() {}};struct derived1 : base {     base* clone() const { return new derived1(*this); }   base* create() const { return new derived1; }   void show() const {      std::cout << "derived1\n";   }};struct derived2 : base {     base* clone() const { return new derived2(*this); }   base* create() const { return new derived2; }   void show() const {      std::cout << "derived2\n";   }};class container {   std::vector<base *> references;public:   void add(const base& b) {      references.push_back(b.clone());   }   void execute () const {      for(int i = 0, n = references.size(); i < n; ++i)         references->show();   }   ~container() {      for(int i = 0, n = references.size(); i < n; ++i)         delete references;   }};int main () {   container stuff;   stuff.add(derived1());   stuff.add(derived2());   stuff.execute();    return 0;}
Quote:Original post by ekrax
so now im actually sending a class type to it not just an integer, but then this line somehow works ...

type *p = new type (object) ...


Well thats fine because your copy constructing the parameter instance on the heap, any instance created with new will persist through out any scope but your responsiable to delete it to give it back.
just let you know i made some mistakes in that code earlier on so i've corrected it now, anyways you don't have to use the virtual constructor idiom if you don't wont to create copies.
This line works because of what is called a copy constructor:

Object::Object( const Object & )

You do not need to write it: if you don't, it is replaced by a default copy constructor that performs a simple (shallow) copy of its argument.
ok yeah i should have realized there was a default copy constructor there. usually when i think of copy constructors i think of passing classes to functions, but i didnt realize that they can be used to initialize objects aswell.

This topic is closed to new replies.

Advertisement