Сначала спасибо :(.
Так что я сейчас делаю домашнее задание, работаю с std :: list и наследую классы.
Я хочу получить сумму объектов в классе A / B / C в std :: list Lists;
Я пытаюсь for_each, но, возможно, я был неправ.
Мои занятия
class Pets
{
protected:
std::string iName;
float iPrice;
public:
virtual float getPrice();
Pets();
virtual ~Pets();
};
class Cat : public Pets
{
std::string iType;
public:
virtual float getPrice();
Cat();
virtual ~Cat();
};
class Dog : public Pets
{
float iWeight;
public:
virtual float getPrice();
Dog();
virtual ~Dog();
};
Это мой Source.cpp
#include "Pets.h"
#include "Dog.h"
#include "Cat.h"
#include <list>
#include <algorithm>
#include <functional>
void main() {
std::list<Pets*> List;
float sum = 0;
std::for_each(List.begin(), List.end(), sum = sum + std::mem_fun(&Pets::getPrice));
system("pause");
}