CPP01 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
zombieHorde.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* zombieHorde.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/04/14 21:07:48 by kamitsui #+# #+# */
9/* Updated: 2025/04/23 01:47:54 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
18#include "Zombie.hpp"
19#include <cstdlib> // For NULL
20#include <iostream>
21
36 if (N <= 0) {
37 return NULL;
38 }
39
40 Zombie *horde = NULL; // Allocate N Zombie objects in a single allocation
41 try {
42 horde = new Zombie[N];
43 } catch (const std::bad_alloc &e) {
44 std::cerr << "Error: Failed to allocate memory for N Zombie in a single allocation." << std::endl;
45 std::cerr << "Reason: " << e.what() << std::endl;
46 return NULL;
47 }
48
49 for (int i = 0; i < N; ++i) {
50 new (&horde[i]) Zombie(name); // Placement new to call constructor
51 }
52
53 return horde; // Return a pointer to the first zombie
54}
basic_ostream< _CharT, _Traits > & endl(basic_ostream< _CharT, _Traits > &__os)
ostream cerr
virtual const char * what() const
Represents a Zombie.
Definition Zombie.hpp:30
Declares the Zombie class and the zombieHorde function.
Zombie * zombieHorde(int N, std::string name)
Creates a horde of Zombies on the heap.