-1

If you have new node; and new node(); do both call the same constructor in the same way? Can there be an implementation where the two would call different constructor?

Also, if I wanted to make multiple instances, i.e. new node [5];, is there a way to do this with an overloaded constructor with passed arguments?

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
Efreeto
  • 2,132
  • 1
  • 24
  • 25
  • 4
    What **language** are you talking about? Please edit your question and add a suitable tag. – Damien_The_Unbeliever Mar 14 '14 at 08:53
  • The best way to learn is to try doing it yourself! – zoska Mar 14 '14 at 09:30
  • 1
    @zoska This is actually quite complicated. It is too easy to draw the wrong conclusions by trying it yourself. – juanchopanza Mar 14 '14 at 10:06
  • You did the right thing by putting the language in a tag. Unfortunately, you also did a less right thing - we don't put tags in the title, unless you can't form a good question without that information. The primary way we categorize questions is by the tags. – Damien_The_Unbeliever Mar 14 '14 at 10:07
  • 1
    possible duplicate of [Do the parentheses after the type name make a difference with new?](http://stackoverflow.com/questions/620137/do-the-parentheses-after-the-type-name-make-a-difference-with-new) – juanchopanza Mar 14 '14 at 10:17

3 Answers3

2
node* n = new node;   // 1
node* n = new node(); // 2

Can there be an implementation where the two would call different constructor?

1 is a default initialization, and 2 is a value initialization. What happens depends on the type of node.

If it is a built-in type, the first version leaves the value of *n un-initialized. The second results in zero-initialization.

If it is an aggregate, 1 would leave data members of built-in type uninitialized, 2 would zero-initialize them. Members of user defined types would get default constructed.

If it is a (non-aggregate) user defined type with a default constructor, the default constructor will be called in both cases.

Also, if I wanted to make multiple instances, i.e. new node [5];

Yes, unless you are stuck with a pre-C++11 compiler. For example,

struct Foo
{
  int i;
  Foo(int i) : i(i) {}
};

Foo* f = new Foo[3] {1, 2, 3};
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

You could very easily check this by writing a simple class that prints something in the constructor.

Yes,

new node;

and

new node();

calls the same constructor.

dutt
  • 7,909
  • 11
  • 52
  • 85
  • What if `node` actually is a `typedef`, such as `typedef int node`? – dalle Mar 14 '14 at 09:49
  • See juanchopanzas answer above. – dutt Mar 14 '14 at 10:05
  • @dutt - just a general note. You should avoid referring to answers (or comments) on SO using positional phrases like "above" or "below", because due to voting and randomization, the position of these items may change. If you want to refer to a specific answer, it's usually better to include a link to it. – Damien_The_Unbeliever Mar 14 '14 at 13:07
  • @Damien_The_Unbeliever Thanks, I'll use links in the future. – dutt Mar 14 '14 at 13:41
0

1st question, new node; and new node(); will call the same default constructor.

2nd question, short answer: use std::vector<node>.

If you have to use an array, you can use a extended initializer lists in C++11:

new node[5]{node(0), node(1), node(2), node(3), node(4)};
songyuanyao
  • 169,198
  • 16
  • 310
  • 405