How to initialize const data members in class with C++

1 Answer

0 votes
#include <iostream>
 
class Test {
    const int n;
    public:
        Test(int a) : n(a) {}
        void show() {
            std::cout << n;
        }
};
 
int main()
{
    Test t(20);
     
    t.show();
     
    return 0;
}
 
  
  
/*
run:
  
20
  
*/

 



answered Dec 11, 2020 by avibootz

Related questions

1 answer 182 views
1 answer 240 views
1 answer 211 views
1 answer 146 views
1 answer 176 views
1 answer 156 views
...