G
Guest
If I have a reference to a temporary, the data of the reference variable is
overwritten in VC++7.0, VC++7.1 and VC++6.0 are working.
After a3 is inizialized a2 contains invalid data. Is this a bug in the
compiler, do I have an error or are there some compiler settings that change
this behaviour?
Unfortunately I can't change to VC++7.1 because of several old iostream
objects used by a library.
Enclosed an example:
#include "string"
#include "iostream"
using namespace std;
class A {
double a[10];
string name;
public:
A(const char* n) {
for(int i=0; i<10; i++) {
a = i;
}
name = n;
}
A(const A& src) {
name = src.name;
for(int i=0; i<10; i++) {
a = src.a;
}
}
void print() {
cout << "printing " << name << endl;
for(int i=0; i<10; i++) {
cout << a << " ";
}
cout << endl;
}
static A NewA(const char* n) {
A tmp(n);
return tmp;
}
};
int main(int argc, char* argv[])
{
A a1("text1");
A& a2=A::NewA("text2");
a2.print();
A a3("text3");
a2.print();
return 0;
}
Thanks for any suggestions
Markus
overwritten in VC++7.0, VC++7.1 and VC++6.0 are working.
After a3 is inizialized a2 contains invalid data. Is this a bug in the
compiler, do I have an error or are there some compiler settings that change
this behaviour?
Unfortunately I can't change to VC++7.1 because of several old iostream
objects used by a library.
Enclosed an example:
#include "string"
#include "iostream"
using namespace std;
class A {
double a[10];
string name;
public:
A(const char* n) {
for(int i=0; i<10; i++) {
a = i;
}
name = n;
}
A(const A& src) {
name = src.name;
for(int i=0; i<10; i++) {
a = src.a;
}
}
void print() {
cout << "printing " << name << endl;
for(int i=0; i<10; i++) {
cout << a << " ";
}
cout << endl;
}
static A NewA(const char* n) {
A tmp(n);
return tmp;
}
};
int main(int argc, char* argv[])
{
A a1("text1");
A& a2=A::NewA("text2");
a2.print();
A a3("text3");
a2.print();
return 0;
}
Thanks for any suggestions
Markus