Will it work?
Yes.
However, if possible, you should use a vector:
#include <vector> std::vector<Ant*> ants; for (int i = 0; i < num_ants; ++i) { ants.push_back(new Ant()); }
If you have to use a dynamically allocated array then I would prefer this syntax:
typedef Ant* AntPtr; AntPtr * ants = new AntPtr[num_ants]; for (int i = 0; i < num_ants; ++i) { ants[i] = new Ant(); }
But forget all that. The code still isn’t any good since it requires manual memory management. To fix that you could to change your code to:
std::vector<std::unique_ptr<Ant>> ants; for (auto i = 0; i != num_ants; ++i) { ants.push_back(std::make_unique<Ant>()); }
And best of all would be simply this:
std::vector<Ant> ants(num_ants);
Related Posts:
- When to use “new” and when not to, in C++?
- Is “delete this” allowed in C++?
- How to dynamically allocate arrays in C++
- How to dynamically allocate arrays in C++
- Expression must have class type
- Use new operator to initialise an array
- What’s the equivalent of new/delete of C++ in C?
- Why use a new call with a C++ ‘vector’?
- When should I use the new keyword in C++?
- Deleting an object in C++
- Conversion from ‘myItem*’ to non-scalar type ‘myItem’ requested
- C++ convert from 1 char to string?
- Pause Console in C++ program
- Initializing an array of objects
- Passing an array by reference
- When should you use a class vs a struct in C++?
- What does int argc, char *argv[] mean?
- Meaning of = delete after function declaration
- Using NULL in C++?
- Parse (split) a string in C++ using string delimiter (standard C++)
- c++ “Incomplete type not allowed” error accessing class reference information (Circular dependency with forward declaration)
- C++ Error: undefined reference to `main’
- 1.#QNAN error C++
- What is the array form of ‘delete’?
- Error: Expression must have integral or unscoped enum type
- Remove secure warnings (_CRT_SECURE_NO_WARNINGS) from projects by default in Visual Studio
- What are C++ functors and their uses?
- invalid conversion from ‘const char*’ to ‘char*’
- Check if a string is palindrome
- What is an ‘undeclared identifier’ error and how do I fix it?
- Is C++ an Object Oriented language?
- Initializing a two dimensional std::vector
- What does “<<" and ">>” mean in C++ for cout/cin?
- C ++ error: a expected initializer before [function name]
- invalid new-expression of abstract class type
- Returning multiple values from a C++ function
- Eclipse C++ : “Program “g++” not found in PATH”
- no default constructor exists for class
- Press Enter to Continue
- How to trim a std::string?
- How to find if a given key exists in a C++ std::map
- Parsing a comma-delimited std::string
- Correct way of looping through C++ arrays
- C++ how to read a line with delimiter until the end of each line?
- Random number c++ in some range
- C++: std does not have member “string”
- Fatal error: ‘stdafx.h’ file not found
- How do I add the MinGW bin directory to my system path?
- What’s the syntax for declaring an array of function pointers without using a separate typedef?
- C++ cannot open source file
- What is the difference between const_iterator and non-const iterator in the C++ STL?
- Sorting Linked List C++ with pointers
- Get Unix timestamp with C++
- Class prototyping
- 8 queens puzzle with user input c++
- QltAW.png
- std::wstring VS std::string
- Is dependency injection useful in C++
- C++ string to enum
- c++ “Incomplete type not allowed” error accessing class reference information (Circular dependency with forward declaration)
- why is string not declared in scope
- How does rhs work?
- error: expected unqualified-id before ‘.’ token //(struct)
- What does “Symbol not found / Expected in: flat namespace” actually mean?
- Base class undefined
- gcc: undefined reference to
- “Field has incomplete type” error
- C++ Returning reference to local variable
- Multidimensional Vectors in C++
- How do I flush the cin buffer?
- C++ Return value, reference, const reference
- Making a countdown timer in C++
- G++ undefined reference to class::function
- Is string::compare reliable to determine alphabetical order?
- How to return a struct from a function in C++?
- C++ printing boolean, what is displayed?
- Error : aggregate ‘first one’ has incomplete type and cannot be defined
- CMake : C and CXX compiler identification is unknown Win10 CMakeGUI
- Error “Unterminated conditional directive” in cross-referencing headers
- OpenMP set_num_threads() is not working
- What is a null-terminated string?
- Right Justifying output stream in C++
- Serial Port (RS -232) Connection in C++
- Fixing Segmentation faults in C++
- how to destroy an object in C++
- Error in assignment of member in read-only object
- creating an array of structs in c++
- What’s the Use of ‘\r’ escape sequence?
- How do I use an enum value in a switch statement in C++?
- Why should I use a pointer rather than the object itself?
- Call of overloaded function is ambiguous
- ‘cout’ does not name a type
- What is the difference between the dot (.) operator and -> in C++?
- Different ways to deallocate an array – c++
- How to determine the version of the C++ standard used by the compiler?
- Declare a bit in C++
- Difference between the int * i and int** i
- Download c++ in existing visual studio 2017
- istream and ostream problem – C++
- List iterator not dereferencable?