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 167 views
1 answer 228 views
1 answer 201 views
1 answer 134 views
1 answer 164 views
1 answer 145 views
...