1

I'm quite new to C++, but have general knowledge of other languages. Lately I've seen some tutorials about C++, and I have sometimes seen classes that do not have their own constructor, not even className();. This might exist in the other languages as well, but I've never seen it before. I don't think I've seen them in use before either, so my question is: what are they for? And what are they? I tried googling this, but I don't know the name for it.. 'constructorless class' didn't give me much.

Without a constructor, is it possible to instantiate it? Or is it more of a static thing? If I have a class that contains an integer, but has no constructor, could I go int i = myClass.int; or something like that? How do you access a constructorless class?

Sti
  • 8,275
  • 9
  • 62
  • 124
  • 1
    You can search for `implicit constructor`. See also [this question](http://stackoverflow.com/questions/563221/is-there-an-implicit-default-constructor-in-c) – Ric Feb 23 '13 at 01:13

3 Answers3

4

If you don't explicitly declare a constructor, then the compiler supplies a zero-argument constructor for you.*

So this code:

class Foo {
};

is the same as this code:

class Foo {
public:
    Foo() {};
};


* Except in cases where this wouldn't work, e.g. the class contains reference or const members that need to be initialized, or derives from a superclass that doesn't have a default constructor.
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • Thanks! About that last part, are you saying that a class that derives from a superclass without explicit constructor MUST have an explicit constructor, or that it is not the same as `Foo() {};` because it CAN'T have an explicit constructor? – Sti Feb 23 '13 at 01:30
  • 1
    @Rheven: I'm saying that if the superclass doesn't have a default constructor, then the subclass needs to explicitly invoke a particular constructor. The compiler doesn't know how to do that, so it can't supply a subclass constructor for you. – Oliver Charlesworth Feb 23 '13 at 01:32
  • I'm not sure I understand what that means.. 'explicitly invoke a particular constructor'? So if the superclass does not have an explicit constructor, the subclass needs something like `public: Subclass(){};`. I can't find anywhere to read up on this.. Tried searching for 'subclass of implicit constructor c++', but only reversed questions and answers.. – Sti Feb 23 '13 at 01:55
  • @OliCharlesworth: If you're going to explain the default constructor like that, I think it'd be better if you showed/mentioned the copy constructor/move assignment/destructor/etc – Mooing Duck Feb 23 '13 at 02:06
  • @Rheven: It's possible to make classes where the compiler does _not_ make a default constructor for you. If another class derives from such a class that has no default constructor _even an automatic one_, then the derived class will also have no automatic default constructor. – Mooing Duck Feb 23 '13 at 02:08
  • @MooingDuck Ok, but since the compiler does make a default constructor implicitly, what happens with the subclass if it also does not explicitly have a constructor? As I understood Oli's last part of the answer, this is an exception and won't work? Usually, a constructor in a subclass also calls the constructor in the superclass, right? `public: Subclass(int i):Superclass(i)` or something like that. How does everything work when both sub- and superclass are without explicit constructor? Sorry, I'm having a hard time understanding these large words as English isn't my native language.. – Sti Feb 23 '13 at 02:17
  • Yes, his little note means: If a superclass or member cannot be constructed with no arguments, then the compiler will _not_ automatically generate a default constructor for the derived class. – Mooing Duck Feb 23 '13 at 02:23
0

If you do not specify a constructor explicitly compiler generates default constructors for you (constructor without arguments and copy constructor). So there is no such thing as constructorless class. You can make your constructor inaccessible to control when and how instances of your class created but that's a different story.

Slava
  • 43,454
  • 1
  • 47
  • 90
0

A class without a constructor is a good model for implementing an interface.

Many interfaces consist of methods and no data members, so there is nothing to construct.

class Field_Interface
{
  public:
    // Every field has a name.
    virtual const std::string&  get_field_name(void) const = 0;

    //  Every field must be able to return its value as a string
    virtual std::string         get_value_as_string(void) const = 0;
};

The above class is know as an abstract class. It is not meant to have any function, but to define an interface.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154