M
Manuel
Hi!
If I've a vector filled with abstract classes, can I push in it the
derived classes too? Even if derived classes have new methods?
I've done some experiments, and it seem I can push the derived classes,
but I can use them only calling the methods declared in abstract class
(the new methods declared only in derived classes return an error).
Please take a look to my code (hey, this is not an homework!
I'm studying C++ to port this opensource project from python to C++
http://www.makehuman.org):
This is my abstract class:
--------------------------------------------
class mhwidget
{
public:
virtual void draw()= 0;
virtual ~mhwidget() {}
};
--------------------------------------------
In another file, I've defined a vector for this class:
--------------------------------------------
std::vector<mhwidget*> widgetList;
--------------------------------------------
I fill this vector, and use the "draw()" method,
in this way:
--------------------------------------------
//Put widget into container
void mhcontainer::addWidget(mhwidget* w)
{
widgetList.push_back(w);
}
//Draw all widgets
void mhcontainer::drawAll()
{
std::vector<mhwidget*>::iterator it = widgetList.begin();
while(it != widgetList.end())
{
(*it++)->draw();
}
}
--------------------------------------------
I've written an example of derived class (this is only the header):
---------------------------------------------
class square : public mhwidget
{
public:
virtual void draw();
};
---------------------------------------------
and this seem to work (from application/client code):
---------------------------------------------
mhcontainer contn;
contn.addWidget(new square);
contn.drawAll();
---------------------------------------------
but I'm not sure it's a clean C++ code.
If I add a new method in "square" and try to use it, like
while(it != widgetList.end())
{
(*it++)->draw();
(*it++)->newMethod();
}
it return an error, but this make sense...
thx,
Manuel
If I've a vector filled with abstract classes, can I push in it the
derived classes too? Even if derived classes have new methods?
I've done some experiments, and it seem I can push the derived classes,
but I can use them only calling the methods declared in abstract class
(the new methods declared only in derived classes return an error).
Please take a look to my code (hey, this is not an homework!
I'm studying C++ to port this opensource project from python to C++
http://www.makehuman.org):
This is my abstract class:
--------------------------------------------
class mhwidget
{
public:
virtual void draw()= 0;
virtual ~mhwidget() {}
};
--------------------------------------------
In another file, I've defined a vector for this class:
--------------------------------------------
std::vector<mhwidget*> widgetList;
--------------------------------------------
I fill this vector, and use the "draw()" method,
in this way:
--------------------------------------------
//Put widget into container
void mhcontainer::addWidget(mhwidget* w)
{
widgetList.push_back(w);
}
//Draw all widgets
void mhcontainer::drawAll()
{
std::vector<mhwidget*>::iterator it = widgetList.begin();
while(it != widgetList.end())
{
(*it++)->draw();
}
}
--------------------------------------------
I've written an example of derived class (this is only the header):
---------------------------------------------
class square : public mhwidget
{
public:
virtual void draw();
};
---------------------------------------------
and this seem to work (from application/client code):
---------------------------------------------
mhcontainer contn;
contn.addWidget(new square);
contn.drawAll();
---------------------------------------------
but I'm not sure it's a clean C++ code.
If I add a new method in "square" and try to use it, like
while(it != widgetList.end())
{
(*it++)->draw();
(*it++)->newMethod();
}
it return an error, but this make sense...
thx,
Manuel