Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ -- Which one is supposed to use "new Car" or "new Car()"? [duplicate]

Tags:

c++

Possible Duplicate:
Do the parentheses after the type name make a difference with new?

Hello all,

class Car
{
public:
    Car() : m_iPrice(0) {}
    Car(int iPrice) : m_iPrice(iPrice) {}

private:
    int m_iPrice;
};

int _tmain(int argc, _TCHAR* argv[])
{
    Car  car1;    // Line 1
    Car  car2();  // Line 2, this statement declares a function instead.

    Car* pCar = new Car; // Line 3
    Car* pCar2 = new Car(); // Line 4

    return 0;
}

Here is my question:

When we define an object of Car, we should use Line 1 rather than Line 2. When we new an object, both Line 3 and Line 4 can pass the compiler of VC8.0. However, what is the better way Line 3 or Line 4? Or, Line 3 is equal to Line 4.

Thank you

like image 924
q0987 Avatar asked Dec 30 '10 16:12

q0987


People also ask

Should you choose a used car or a new car?

If you are trying to save money on your initial purchase, a used car is a good choice. But you'll need to consider the cost over the life of ownership — including maintenance and repairs. You may find that buying new is the better choice for your finances.

What should you replace first on a used car?

Start with the standard fluid swaps — new engine oil, new brake fluid — and expand that to include a radiator coolant flush, as well as a check and possible replacement of the automatic transmission fluid (if required) or manual transmission oil.

What makes a new car new?

For tax purposes, the IRS generally defines a new car as one that has never been registered for personal use with the state Department of Motor Vehicles (DMV). Historians generally consider the first new car to have been sold in the 1890s, although the specific candidates for this title are always in debate.


2 Answers

In this case the lines are equivalent; there's no difference in operation.

However, please be careful - this does not mean that such lines are ALWAYS equialent. If your data type is a POD (i.e. a simple struct:

struct Foo
{
    int a;
    float b;
};

Then new Foo produces a uninitialized object, and new Foo() calls a value initalization constructor, which value-initializes all fields - i.e. sets them to 0 in this case.

Since in such cases it's easy to accidentally call new Foo(), forget to initialize objects (this is fine!), and then accidentally make your class non-POD (in which case the value-initialization will not be done and the object will be uninitialized again), it's slightly better to always call new Foo (though this produces a warning in some MSVC versions).

like image 126
zeuxcg Avatar answered Nov 13 '22 14:11

zeuxcg


It makes no difference when the type you new has a user declared constructor.

If it doesn't, then Line 4 will initialize all the members to zero first (but only call constructors for those members that have user declared constructors) and do the same to base classes. If you don't want to have that done, use Line 3. This rule also takes place for complex objects with virtual member functions, base classes and so on - only condition is whether or not the class has a user declared constructor.

This is a very subtle corner of C++, and I think I wouldn't base my code on these facts without having a comment explaining that in the code (oh dear, my colleagues will get mad at me otherwise).

like image 34
Johannes Schaub - litb Avatar answered Nov 13 '22 15:11

Johannes Schaub - litb