A new way to assign variable in cpp is:
datatype variable name(any data)
#include<bits/stdc++.h>
using namespace std;
int main(){
int b = 11;
int a(10);
cout<<"new way to print "<<a<<endl;
cout<<"old way to print "<<b<<endl;
}
Output shown here:
➜ ~ g++ test.cpp
➜ ~ ./a.out
new way to print 10
old way to print 11
Top comments (1)
Yeah it was introduced I think in C++17. It's called constructor initialization (named after how variables are initialized in class constructors). Another way is uniform initialization:
int a {10};
. Both of these ways can be combined with the old way:int a = (10)
andint a = {10}
.