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?

1
  • 1
    You can search for implicit constructor. See also this question Commented Feb 23, 2013 at 1:13

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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?
@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.
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..
@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
@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.
|
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.

1 Comment

It's possible to explicitly delete all of the constructors though, or make them all private.
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.

1 Comment

This class still has a constructor.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.