Can a default constructor initialize data members in c++?
Can a default constructor initialize data members in c++?
yes; initializing data members is pretty much the main job of any constructor, defaut or not.
e.g.
1 2 3 4 5 6 7 8 9 10
|
#include <iostream>
struct Example {
int member;
Example() : member(7) {}
};
int main()
{
Example e;
std::cout << e.member << '\n';
}
|
Topic archived. No new replies allowed.