473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Reference to temporary

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] = i;
}
name = n;
}
A(const A& src) {
name = src.name;
for(int i=0; i<10; i++) {
a[i] = src.a[i];
}
}
void print() {
cout << "printing " << name << endl;
for(int i=0; i<10; i++) {
cout << a[i] << " ";
}
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
Nov 17 '05 #1
4 995
Markus Raabe wrote:
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
Nov 17 '05 #2
> 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
Nov 17 '05 #3
Markus Raabe wrote:
That's a bug in VC7.
Is there somewhere a list of bugs in VC7 available?


Sadly, no.
Is VC7.1 better?


In every way (IMO), yes.

-cd
Nov 17 '05 #4
Markus Raabe wrote:
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
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ded' | last post by:
Hello ! I've read in a magazine "reference parameter in operator= must be const, because in C++, temporary objects are const" and then my operator would not work with temporary objets. But,...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
6
by: JKop | last post by:
unsigned int CheesePlain(void) { unsigned int const chalk = 42; return chalk; } unsigned int& CheeseRef(void) {
14
by: Arik Funke | last post by:
Hi together, following code does compile fine on MS VC but not on gcc. What am I doing wrong? Is there a universal coding standard? Cheers, Arik --- class abc {
10
by: sam | last post by:
Hi, I m wondering why I can't declare reference variable for default value in a function argument variable? eg. class A { void f(string &str="");
8
by: Tim Clacy | last post by:
1) Is this initialising the reference 'u' to the address of the literal '2' or to the address 0x00000002? unsigned const& u = 2; 2) What is the different between the initialisation of 'u'...
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
7
by: Johannes Bauer | last post by:
Hello Group, please consider the following code #include <vector> #include <iostream> #define USE_CONST #define USE_STRING
5
by: George2 | last post by:
Hello everyone, This is my understanding of non-const reference, const reference and their relationships with lvalue/rvalue. Please help to review whether it is correct and feel free to...
3
by: George2 | last post by:
Hello everyone, 1. Returning non-const reference to function local object is not correct. But is it correct to return const reference to function local object? 2. If in (1), it is correct...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.