Why experienced C++ developers advise avoiding the use of `new`
Hi! I'm transitioning from Java to C++ and I'm having some confusion. In Java, we constantly use `new` to create objects—it's standard practice. But reading forums and articles, I see that many experienced C++ developers advise "minimizing the use of `new`." Honestly, this sounds strange.
Here's an example I wrote, following my Java habits:
```cpp
#include
#include
class Person {
public:
Person(const std::string& name) : name_(name) {}
void greet() const { std::cout << "Hello, I'm " << name_ << std::endl; }
private:
std::string name_;
};
int main() {
Person* p = new Person("Alice");
p->greet();
// Forgot delete? Or do I need to call it somewhere?
Person* p2 = new Person("Bob");
p2->greet();
// And if
No answers yet.
Log in to leave an answer
Blogs
Logging in Rust Applications: Best Practices for Configuration
Go Application Security: Top 10 Vulnerabilities for Beginners
Concatenating Strings in Rust: TOP-5 Beginner Mistakes (and How to Avoid Them)
CI/CD pipeline for Swift: setup from scratch on macOS and Linux