I’m trying to compile the code in Visual Studio (2008) and g++.
In vs2008 it’s successful, but in g++ it reported error.
if add const,
test(const test &source):a(source.a) {}
g++ will compiled succeed.
I kown that test aa = 2; will create a temporary object and call copy-constructor.
temporary object cannot bind to a non-const reference
so,why the vs2008 can compiled it succeed?
class test{
public:
test():a(1) {}
test(int num):a(num) {}
test(test &source):a(source.a) {}
private:
int a;
};
int main(){
test aa = 2;
return 0;
}
VS has a non-standard extension that allows it, unfortunately.
There’s a compiler flag to disable extensions, but last I checked it also makes it impossible to use the standard library. Your best bet is to keep the Warning Level on 4 (though this particular situation gets no warning), and check your compilations with multiple compilers when possible.