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 188 views
1 answer 246 views
1 answer 215 views
1 answer 152 views
1 answer 181 views
1 answer 161 views
...