Can a default constructor initialize data members in c++?

Nov 5, 2013 at 5:00am
Can a default constructor initialize data members in c++?
Nov 5, 2013 at 5:10am
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.