Lesson 3 | What is a constructor? |
Objective | Explore the use of constructor member functions. |
How are C++ Constructors used?
Each class defines how objects of its type can be initialized. Classes control object initialization by defining one or more special member functions known as constructors. The job of a constructor is to initialize the data members of a class object. A constructor is run whenever an object of a class type is created.
In this section, we will introduce the basics of how to define a constructor. Constructors have the same name as the class. Unlike other functions, constructors have no return type. Like other functions, constructors have a (possibly empty) parameter list and a (possibly empty) function body. A class can have multiple constructors. Like any other overloaded function, the constructors must differ from each other in the number or types of their parameters.
Unlike other member functions, constructors may not be declared as const .
When we create a const object of a class type, the object does not assume its "constness" until after the constructor completes the object' s initialization.
Thus, constructors can write to const objects during their construction.
C++ Primer
C++ Constructor Member Functions
A
constructor is a member function whose name is the same as the
class
name. It constructs objects of its class type.
This process involves initializing data members and, frequently, allocating free store using
new
to create an object.
- Constructors:
- Can be overloaded and can take arguments,
- Are invoked when their associated type is used in a definition
- Are invoked when call-by-value is used to pass a value to a function, or when the return value of a function must create a value of associated type
Difference between a Constructor "being invoked in a class" versus "in a definition"
In C++, the constructor of a class is a
special member function that is invoked automatically when an object of that class is created. The primary role of a constructor is to initialize the class's data members. Typically, when we talk about a constructor being "invoked in a class," we are referring to the declaration and definition of the constructor within the class's definition.
Here's an example
class MyClass {
public:
MyClass() { // Constructor definition within the class definition
// Initialization code
}
};
In contrast, the term "invoked in a definition" is likely referring to the instantiation of an object of the class, which triggers the constructor.
For example:
MyClass obj; // Constructor invocation during object definition
In code above,
MyClass obj;
, we are defining an object named obj of type MyClass. This statement invokes the MyClass constructor, creating an instance of MyClass.
In summary, a constructor is defined within a class and invoked when an object of that class is defined. These are complementary aspects of how constructors function in C++, rather than contrasting scenarios. The constructor must be defined within the class before it can be invoked by the definition (creation) of an object.
- Invoking a C++ Constructor:
A constructor is invoked when its associated class is used in a definition. Not all declarations are definitions.
A constructor is not invoked when the class is used in a declaration that is not a definition. If we have a class foo
, then
foo y(3);
invokes the foo()
constructor, while
extern foo x;
does not invokes the foo()
constructor.
Constructor function in C++
The class designer can guarantee initialization of every object by providing a special function called the constructor. If a class has a constructor, the compiler automatically calls that constructor at the point an object is created, before client programmers can get their hands on the object. The constructor call is not even an option for the client programmer; it is performed by the compiler at the point the object is defined. The next challenge is what to name this function. There are two issues.
- The first is that any name you use is something that can potentially clash with a name you might like to use as a member in the class.
- The second is that because the compiler is responsible for calling the constructor, it must always know which function to call.
The solution Stroustrup chose seems the easiest and most logical: the name of the constructor is the same as the name of the class. It
makes sense that such a function will be called automatically on initialization. Here is a simple class with a constructor:
class X {
int i;
public:
X(); // Constructor
};
Now, when an object is defined,
void f() {
X a;
// ...
}
the same thing happens as if a were an int: storage is allocated for the object. But when the program reaches the sequence point (point of execution) where a is defined, the constructor is called automatically. That is, the compiler quietly inserts the call to X::X( ) for the object a at the point of definition. Like any member function, the first argument to the constructor is the this pointer (the address of the object for which it is being called). In the case of the constructor, however, this is pointing to an un-initialized block of memory, and it is the job of the constructor to initialize this memory properly Like any function, the constructor can have arguments to allow you to specify how an object is created, give it initialization values, and so on. Constructor arguments provide you with a way to guarantee that all parts of your object are initialized to appropriate values. For example, if a class Tree has a constructor that takes a single integer argument denoting the height of the tree, then you must create a tree object like this:
Tree t(12); // 12-foot tree
If Tree(int) is your only constructor, the compiler will not let you create an object any other way.