Created
June 6, 2024 20:51
-
-
Save num8er/66ebdd48110246abb17c6a5f2316a1ab to your computer and use it in GitHub Desktop.
Experimenting with Object (Product) storage (Warehouse) of fixed size and smart pointers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
struct Product { | |
Product(const std::string& name): name(name) { | |
std::cout << "Named Product constructor for \"" + name + "\"" << std::endl; | |
} | |
Product(): name("") { | |
std::cout << "Draft Product constructor" << std::endl; | |
} | |
~Product() { | |
std::cout << "Product destructor for \"" + name + "\"" << std::endl; | |
} | |
std::string name; | |
}; | |
class Warehouse { | |
public: | |
Warehouse(): m_size(0), m_capacity(3) { | |
std::cout << "Warehouse constructor" << std::endl; | |
m_products = std::make_unique<Product[]>(m_capacity); | |
} | |
~Warehouse() { | |
std::cout << "Warehouse destructor" << std::endl; | |
} | |
void add_product(const Product& product) { | |
if (m_size < m_capacity) { | |
m_products.get()[m_size] = product; | |
m_size++; | |
} | |
} | |
void print_products() { | |
for (int i = 0; i < m_size; i++) { | |
std::cout << m_products.get()[i].name << std::endl; | |
} | |
} | |
private: | |
std::size_t m_size; | |
std::size_t m_capacity; | |
std::unique_ptr<Product[]> m_products; | |
}; | |
int main() { | |
Warehouse warehouse; | |
warehouse.add_product(Product("TEST PRODUCT 1")); | |
warehouse.add_product(Product("TEST PRODUCT 2")); | |
warehouse.print_products(); | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
struct Product { | |
Product(const std::string& name): name(name) { | |
std::cout << "Named Product constructor for \"" + name + "\"" << std::endl; | |
} | |
Product(): name("") { | |
std::cout << "Draft Product constructor" << std::endl; | |
} | |
~Product() { | |
std::cout << "Product destructor for \"" + name + "\"" << std::endl; | |
} | |
std::string name; | |
}; | |
class Warehouse { | |
public: | |
Warehouse(std::size_t capacity) : m_size(0), m_capacity(capacity) { | |
std::cout << "Warehouse constructor for " + std::to_string(m_capacity) + " items" << std::endl; | |
m_products.resize(m_capacity); | |
} | |
~Warehouse() { | |
std::cout << "Warehouse destructor" << std::endl; | |
} | |
void add_product(const Product& product) { | |
if (m_size < m_capacity) { | |
m_products[m_size] = std::make_unique<Product>(product); | |
m_size++; | |
} | |
} | |
void print_products() { | |
for (std::size_t i = 0; i < m_size; i++) { | |
if (m_products[i]) { | |
std::cout << m_products[i]->name << std::endl; | |
} | |
} | |
} | |
private: | |
std::size_t m_size; | |
std::size_t m_capacity; | |
std::vector<std::unique_ptr<Product>> m_products; | |
}; | |
int main() { | |
Warehouse warehouse(1000000); | |
warehouse.add_product(Product("TEST PRODUCT 1")); | |
warehouse.add_product(Product("TEST PRODUCT 2")); | |
warehouse.print_products(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment