10

Lets say you are creating an array of objectes on the heap like so:

myClass * objectPtr = new myClass[10];

new only invokes the default constructor, and (based on my readings) does not allow any other constructor to be invoked.

Is there any logic behind why new cannot invoke any other constructor? It would seem better to do something like

myClass * objectPtr = new myClass[10](12);

as opposed to

myClass * objectPtr = new myClass[10];
objectPtr[0] = myClass(12);
objectPtr[1] = myClass(12);
...
1110101001
  • 4,662
  • 7
  • 26
  • 48
  • 3
    `-1` for raw pointer and `new` in one line of code. Also the question isn't particularly reasonable, considering it's not only using raw pointers, decay and useless allocations, but also focuses on raw C array creation which is basically moot. IOW the answer could be "because don't use C arrays" (and all aforementioned stuff). – Bartek Banachewicz Jun 10 '14 at 20:52
  • 2
    FWIW, one can do this with a `std::vector` E.g `std::vector foo(10,myClass(12));` – AndyG Jun 10 '14 at 20:53
  • _'Why does C++ not allow `new` to call constructor when creating arrays'_ Because this doesn't fit the actual syntax of the `new[]()` operator. – πάντα ῥεῖ Jun 10 '14 at 20:57
  • @πάνταῥεῖ That depends on the meaning of "does not allow" – juanchopanza Jun 10 '14 at 20:58
  • @juanchopanza Yes, using brace initializers is probably a good idea ... – πάντα ῥεῖ Jun 10 '14 at 20:59
  • 6
    Why did this question get so many upvotes? sympathy? You guys know you shouldn't countervote against other votes, don't you? Also it does allow constructor calls, so the question is based on a wrong premise to start off. – AliciaBytes Jun 10 '14 at 20:59
  • 5
    @RaphaelMiedl I upvoted because "valid question". It would be even more interesting if it were tagged C++03. We're allowed to write `new int[5]()`, so why not `new int[5](42)`? – jrok Jun 10 '14 at 21:06
  • 1
    @jrok valid vote from you then, just that the +14/-4 seems a bit off for a rather basic question. Given that 1. there are some ways, as juanchopaza points out and 2. it's asking for a reason to language design which doesn't seem too good as a question to me... – AliciaBytes Jun 10 '14 at 21:10
  • 2
    possible duplicate of [c++ Object array initialization without default constructor](http://stackoverflow.com/questions/4754763/c-object-array-initialization-without-default-constructor) – uchar Jun 10 '14 at 21:11
  • @RaphaelMiedl I think it would be ok with a `language-lawyer` tag. What struck me was mostly impracticality of it. – Bartek Banachewicz Jun 10 '14 at 21:11
  • 4
    @jrok That's exactly one of my problem with this question. Questions asking for why aren't usually answerable in a good way. You can see that none of the answers really focus on the "why" part either. – AliciaBytes Jun 10 '14 at 21:15
  • @RaphaelMiedl Usualy, perhaps, [but not always](http://stackoverflow.com/questions/1628768/why-does-an-overridden-function-in-the-derived-class-hide-other-overloads-of-the). The answer to "why there's name hiding of names in base classes" could very well be "because spec says so", but then there are awesome answers as the one in the link. – jrok Jun 10 '14 at 21:18

2 Answers2

15

Why does C++ not allow new to call constructor when creating arrays

It does. It is just a bit tedious:

struct my_class
{
    my_class() {}
    my_class(int, int) {}   
};

int main() 
{
  my_class* objectPtr = new my_class[3]{my_class(1,2),
                                        my_class(), 
                                        my_class(3,4)};
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 2
    Does that construct in place or invoke a copy operator on the created objects? – Jack Aidley Jun 10 '14 at 21:01
  • Except if you declare an array of 100 objects you will manually type out `my_class(a,b)` 100 times. Assuming that you are initializing all of the created objects with the same fields, is there a reason C++ doesn't have any shortened notation for it? – 1110101001 Jun 10 '14 at 21:03
  • 2
    @user2612743 That is why I said "tedious". I can only speculate as to the reason, and that would be that people shouldn't be using this stuff anyway, but `std::vector` instead. – juanchopanza Jun 10 '14 at 21:06
  • 1
    @user2612743, If I absolutely needed that (although I don't see why I ever would), I'd use a `BOOST_PP` macro and a comment to be honest. – chris Jun 10 '14 at 21:12
  • 4
    Note that this won't work for an array who's size is not known at compile time. And if you have a dynamic array, I would think that is the common case. – Benjamin Lindley Jun 10 '14 at 21:30
  • 4
    Note that you [don't have to repeat the type](http://coliru.stacked-crooked.com/a/072ecaf5a031ad75) 5 times. – dyp Jun 10 '14 at 22:49
-2

Usually you won't want to construct the same object many times, therefore you construct many default objects and with a for loop you can change them to your needs.

Another way you can use is:

myClass **ObjectPointers = new myClass*[NUMBER_OF_OBJECTS];
for (int i=0; i<NUMBER_OF_OBJECTS; ++i)
    ObjectPointers[i] = new myClass(1,2,3,...); # <-- non-default constructor

This way you dynamically allocate pointers to objects which you can construct seperately.

user3175215
  • 88
  • 1
  • 5
  • 3
    -1: This reminds me how I wrote my code when I was quite young, after reading my first and only "teach-me-c++" book which I somewhat misunderstood due to earlier C knowledge. Please, seriously, read at least about std::vectors, std::lists and smartpointers before you are back to C++. Believe me, it will save you lots of headaches – quetzalcoatl Jun 11 '14 at 00:14
  • I know all about the STL libraries, thanks. However, I'm a student of Computer Science and when we practice C++ code we are instructed not to use STL. I fully agree though. – user3175215 Jun 11 '14 at 05:37