using shared_ptr

This commit is contained in:
Martin Brodbeck 2018-07-12 08:26:03 +02:00
parent 92fa2ef42a
commit 2131f12237
4 changed files with 13 additions and 13 deletions

View file

@ -22,7 +22,7 @@ inline void Seller::setNumberOfOfferedArticles(int number) { numberOfOfferedArti
int Seller::getNumberOfOfferedArticles() const { return static_cast<int>(articles_.size()); } int Seller::getNumberOfOfferedArticles() const { return static_cast<int>(articles_.size()); }
void Seller::addArticle(Article article) { articles_.push_back(article); } void Seller::addArticle(std::shared_ptr<Article> article) { articles_.push_back(article); }
std::string Seller::getFirstName() const { return firstName_; } std::string Seller::getFirstName() const { return firstName_; }
@ -30,14 +30,14 @@ std::string Seller::getLastName() const { return lastName_; }
int Seller::getSellerNo() const { return sellerNo_; } int Seller::getSellerNo() const { return sellerNo_; }
std::vector<Article*> Seller::getArticles(bool onlySold) std::vector<std::shared_ptr<Article>> Seller::getArticles(bool onlySold)
{ {
std::vector<Article*> articles; std::vector<std::shared_ptr<Article>> articles;
for (auto& article : articles_) { for (const auto article : articles_) {
if (onlySold && article.isSold()) { if (onlySold && article->isSold()) {
articles.push_back(&article); articles.push_back(article);
} else if (!onlySold) { } else if (!onlySold) {
articles.push_back(&article); articles.push_back(article);
} }
} }
return articles; return articles;

View file

@ -4,6 +4,7 @@
#include "article.h" #include "article.h"
#include "entity.h" #include "entity.h"
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -20,20 +21,20 @@ class Seller : public Entity
void setFirstName(const std::string& firstName); void setFirstName(const std::string& firstName);
void setLastName(const std::string& lastName); void setLastName(const std::string& lastName);
void setNumberOfOfferedArticles(int number); void setNumberOfOfferedArticles(int number);
void addArticle(Article article); void addArticle(std::shared_ptr<Article> article);
std::string getFirstName() const; std::string getFirstName() const;
std::string getLastName() const; std::string getLastName() const;
int getSellerNo() const; int getSellerNo() const;
int getNumberOfOfferedArticles() const; int getNumberOfOfferedArticles() const;
std::vector<Article*> getArticles(bool onlySold = false); std::vector<std::shared_ptr<Article>> getArticles(bool onlySold = false);
private: private:
int sellerNo_{-1}; int sellerNo_{-1};
int numberOfOfferedArticles_{}; int numberOfOfferedArticles_{};
std::string firstName_{}; std::string firstName_{};
std::string lastName_{}; std::string lastName_{};
std::vector<Article> articles_{}; std::vector<std::shared_ptr<Article>> articles_{};
}; };
#endif #endif

View file

@ -9,7 +9,6 @@
BOOST_AUTO_TEST_CASE(create_database) BOOST_AUTO_TEST_CASE(create_database)
{ {
Database db(":memory:"); Database db(":memory:");
BOOST_CHECK_NO_THROW(db.init()); BOOST_CHECK_NO_THROW(db.init());
} }

View file

@ -31,8 +31,8 @@ BOOST_AUTO_TEST_CASE(create_many)
BOOST_AUTO_TEST_CASE(with_article) { BOOST_AUTO_TEST_CASE(with_article) {
Seller seller("Max", "Mustermann"); Seller seller("Max", "Mustermann");
Article article{}; auto article = std::make_shared<Article>();
article.setDescription("Test article"); article->setDescription("Test article");
seller.addArticle(article); seller.addArticle(article);
BOOST_TEST(seller.getArticles().at(0)->getDescription() == "Test article"); BOOST_TEST(seller.getArticles().at(0)->getDescription() == "Test article");
} }