views:

71

answers:

2

Should SomeClass* initialEl = new SomeClass[5]; necessarily compile, assuming SomeClass does not have a non-publicly declared default constructor? Consider:

/*
 * SomeClass.h
 *
 */

#ifndef SOMECLASS_H_
#define SOMECLASS_H_

class SomeClass
{

public:
    SomeClass(int){}
    ~SomeClass(){}
};

#endif /* SOMECLASS_H_ */


/*
 * main.cpp
 *
 */

#include "SomeClass.h"

int main()
{
    SomeClass* initialEl = new SomeClass[5];

    delete[] initialEl;

    return 0;
}
+5  A: 

Assuming SomeClass has a publicly accessible default constructor, yes.

Note that there is a difference between

  1. having a publicly accessible default constructor (what i said) and
  2. not having a non-publicly declared default constructor (what you said)

For the following class 2. is true but 1. is not:

class A {
    SomeClass(const SomeClass&) {}
};

This is due to §12.1/5 (C++03):

If there is no user-declared constructor for class X, a default constructor is implicitly declared. An implicitly-declared default constructor is an inline public member of its class.


With your update, SomeClass doesn't have a default constructor. You didn't declare one and because you have declared another constructor the compiler won't declare it implicitly either.

If you need one you have to implement it yourself:

class A {
public:
    SomeClass(int) {}
    SomeClass() {}
};

Or let another constructor qualify as a default constructor:

class A {
public:
    SomeClass(int=0) {}
};
Georg Fritzsche
I'm not so sure.
@dre: Why.... ?
Georg Fritzsche
I have trouble compiling the code I posted above.
@drenami: I'm deleting my answer since this one covers it all (great answer). In response to "I have an implcit one. My compiler even tells me so.": You have no implicit constructor since you explicitly made a constructor yourself. The compiler is wrong if it says you have one. (Though I doubt it's actually saying you do.)
GMan
@GMan: your answer had almost no overlap with Georg's (and little to do with my question), but OK.
@drenami: Ha, ok. I think it's you who doesn't understand and therefore sees them as different. :) His answer is mine plus the why.
GMan
@GMan: I guess my answer is too long anyway ;)
Georg Fritzsche
@GMan: Your answer was about std::vector.@Fritzsche: ;)
@drenami: As a solution with your code, I gave the same reasons as Georg and Jon.
GMan
+1  A: 

No, it won't compile without a default constructor. There is no compiler-generated default constructor in this case, because you have defined another constructor. "The compiler will try to generate one if needed and if the user hasn't declared other constructors." -- The C++ Programming Language, Stroustrup

If you really want to use new SomeClass[5], you'll have to provide a default constructor as well.

Jon Reid
That is not a default constructor. That is a copy constructor.
Jon Reid
Oh OK! But explicitly declared?
Last sentence referred to a deleted comment.
You must explicitly declare it only because you already explicitly declare another constructor. If there were no constructors, it would make one up. But because you've provided one, it won't. You _could_ alter your current constructor to take a default argument, `SomeClass(int x=0)` because a default constructor can _take_ arguments as long as you can call it _without_ arguments.
Jon Reid
Oh, I also recommend you declare your current constructor as `explicit` to prevent unwanted type conversion.
Jon Reid