Reference to temporary

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
 
C

Carl Daniel [VC++ MVP]

Markus said:
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?

That's a bug in VC7.
Unfortunately I can't change to VC++7.1 because of several old
iostream objects used by a library.

Unfortunate. Maybe it's time to update the code to not depend on
pre-standard IOStreams, or change the design to not rely on a reference to
temporary extending the lifetime of said temporary.

-cd
 
G

Guest

That's a bug in VC7.Is there somewhere a list of bugs in VC7 available? Is VC7.1 better? We
already deleted several of these references to temporaries. And we will
change to a newer version too, but this needs time to eliminate old iostreams.

Best regards
Markus
 
T

Tom Widmer

Markus said:
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?

Well, the code is ill-formed, and the compiler should diagnose this.
Unfortunately I can't change to VC++7.1 because of several old iostream
objects used by a library.

Enclosed an example:
static A NewA(const char* n) {
A tmp(n);
return tmp;
}
};
A a1("text1");
A& a2=A::NewA("text2");

The line above is ill-formed, since you are trying to bind a temporary
to a non-const reference. Try compiling with /Za, turning up the warning
level, or making a2 a const reference (and update your print function to
be a const one).

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top