Avoid Redundant Parentheses in Object Instantiation

Avoid Redundant Parentheses in Object Instantiation

Although the following statements are semantically equivalent

   string * p = new string(); //with empty parentheses  string * p = new string; //no parentheses

The second one is preferable for two reasons. First, the parentheses are redundant anyway because they do not contain any arguments. More importantly, programmers who use the first form are often inclined to use empty parenthesis when creating local automatic objects, as in:

   string s(); //probably and error

The programmer mistakenly assumed that this statement creates a local string object but it doesn’t. In fact, this statement is interpreted as a declaration of a function named s, which returns a string object by value and takes no arguments. This is very different from the following:

   string s;  

This statement, on the other hand, creates an object named s of type string, which is what the programmer originally intended. As you can see, the bad habit of placing a redundant pair of empty parentheses when using new can be dangerous. Avoid it.

Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved