I am reading about constructors in C++. I came across this example:
#include <iostream>
using namespace std;
class NoDefault
{
public:
NoDefault(const std::string&);
};
struct A
{
NoDefault my_mem;
};
int main()
{
A a;
return 0;
}
It is giving this message on compilation:
main.cpp:26:7: error: use of deleted function ‘A::A()’
I can get a intuitive feeling that the default ctor is deleted because there is a member of class type inside the struct A
. I want to ask why there is a necessity to initialize the class type member? Can we not leave it uninitialized?
Maybe very trivial question, but I am curious about the thinking behind designing it that way? I am new to OOP.