0
non-static data member initializers only available with -std=c++11 or -std=gnu++11..what does this mean?
I've initialized the variable in private assessor and then used them in public..Although the program ran successfully but the compiler is giving this warning..what does this mean..couldn't understand the complex explanation on stackoverflow
8 Respuestas
+ 1
#include <iostream>
using namespace std;
class operations{
public :
operations(): x{4},y{2} {}
int sum,sub;
void op(){
int a,b;
a=x;
b=y;
sum=a+b;
sub=a-b;
cout<<sum<<endl<<sub;
}
private :
int x,y;
};
int main() {
operations op;
op.op();
return 0;
}
+ 1
-std=c++11 is the compilation macro for c++11. when you have c++11 available on your setup and if you want to compile a code for c++11 you can use that macro.
gcc exam.cpp -std=c++11
+ 1
you can't initialize a member in the class definition unless it's static, const, so you should either initialize the x and y member through a constructor initializer list, or enable C++11 using the flags -std=c++11 or -std=gnu++11
+ 1
Iterator
Before C++11, the available ways to initialize non-static data members* were using class' constructor as the initializer like
class A {
public:
A() {
a = 10;
}
private:
int a;
};
or
class A {
public:
A(int n) : a(n) {}
private:
int a;
};
int main() {
A obj(10);
}
After C++11, another method of doing so also added which was somehow more natural to the way we are initializing local variables. And that what you have used in your class. So, your particular compiler issued a warning** regarding to availability of the later method of initialization in C++11 and above.
_____
* Those data members that don't bound to the class instances which means they are shared between all instances.
** For C++98/03 it will issue an error instead.
0
could you share your code?
0
It is not giving any warning here on this app but whenever i run it on laptop dev c++ , it shows warning although it runs successfully.
https://code.sololearn.com/cV0AnZ2muvx4/?ref=app
0
Mohammed Elomari
0
can you explain the reason also? why should we do it?
Mohamed Elomari