Witam.
Mam problem ze swoją aplikacją, oto jej kod:
#include <iostream>
#include <vector>
#include <string>
#include <memory>
using namespace std;
class BaseClass
{
public:
BaseClass(std::string name, std::string secondName) :
m_name(name), m_secondName(secondName)
{
}
void log()
{
std::cout
<< "Name: "
<< this->m_name
<< " SecondName: "
<< this->m_secondName
<< std::endl;
}
protected:
std::string m_name;
std::string m_secondName;
};
class SubClass : public BaseClass
{
public:
SubClass(BaseClass* baseItemPtr) :
BaseClass(*baseItemPtr),
m_age(0)
{
}
void setAdditionalValue(const int value)
{
this->m_age = value;
}
void logX()
{
std::cout
<< "Name: "
<< this->m_name
<< " SecondName: "
<< this->m_secondName
<< " Age: "
<< this->m_age
<< std::endl;
}
private:
int m_age;
};
void symulation(std::vector<std::shared_ptr<BaseClass>>& vectorItem)
{
for (std::shared_ptr<BaseClass>& item : vectorItem)
{
SubClass* ptr = (SubClass*)item.get();
ptr->logX();
}
}
int main(int argc, char *argv[])
{
BaseClass item("Bolek", "Lolek");
SubClass itemSub(&item);
itemSub.setAdditionalValue(32);
SubClass itemSubB(&item);
itemSubB.setAdditionalValue(44);
std::vector<std::shared_ptr<BaseClass>> myVec;
myVec.push_back(std::shared_ptr<BaseClass>(&itemSub));
myVec.push_back(std::shared_ptr<BaseClass>(&itemSubB));
symulation(myVec);
return 0;
}
No i moje pytanie brzmi: skąd bierze się ta asercja pod koniec wykonywania programu? O czym zapomniałem?
Screen w załączniku.
Będę niezmiernie wdzięczny za szybką odpowiedź.
Pozdrawiam.
- do asercji.png (62 KB) - ściągnięć: 209