DEV Community

yeonseong
yeonseong

Posted on

When Constructors is called in C++

Constructor

Constructor in C++ is a special non-static member function of a class that is used to initialize objects of its class type as cpp reference say.

it means Constructors dose not construct anywhere, Constructor is just invoked at the time of object creation.

Constructor is just function that is invoked automatically at the time of object creation. so you can invoke other function in constructor.

example

ClassName::init(const int& id_in) // other function
{
    m_id = id_in;
}

ClassName::ClassName() // constructor
{
    init(42);
}
Enter fullscreen mode Exit fullscreen mode

reference

https://www.geeksforgeeks.org/constructors-c/

https://en.cppreference.com/w/cpp/language/constructor

Top comments (1)

Collapse
 
kth2624 profile image
Intra id : tkim

good