473,654 Members | 3,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Contradictory behaviour of temporary objects

Hi All,

I find following C++ behaviour regarding temporary objects quite
contradictory. Consider this:

class A
{
...

public:
void bar () { ... } // non-const member function
};

A getA () { ... } // return an object

void foo (A& a) { ... }

int main ()
{
foo (getA ()); // error: not allowed to pass temporary object as
non-const reference. acceptable.

getA ().bar (); // why is this allowed ?? calling a non-const
functrion on a temporary object ?
}

Any ideas why it could be so ?

If passing a temporary object as non-const reference is not allowed
because making changes to it doesn't make sense, doesn't same thing
apply to calling non-const member function on a temporary ?

Jun 10 '07 #1
1 1486
On 2007-06-10 12:15, sa**********@gm ail.com wrote:
Hi All,

I find following C++ behaviour regarding temporary objects quite
contradictory. Consider this:

class A
{
...

public:
void bar () { ... } // non-const member function
};

A getA () { ... } // return an object

void foo (A& a) { ... }

int main ()
{
foo (getA ()); // error: not allowed to pass temporary object as
non-const reference. acceptable.

getA ().bar (); // why is this allowed ?? calling a non-const
functrion on a temporary object ?
}

Any ideas why it could be so ?

If passing a temporary object as non-const reference is not allowed
because making changes to it doesn't make sense, doesn't same thing
apply to calling non-const member function on a temporary ?
No, this has nothing to do with what you can and can not do with
temporary objects, but rather what you can and can not do with
references. A temporary object is just like any other object. However
you can not bind a non-const reference to a temporary since there's a
possibility that you'd end up with stale references (ie. the reference
is still there when the temporary is not).

--
Erik Wikström
Jun 10 '07 #2

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

Similar topics

36
2278
by: Ioannis Vranos | last post by:
For the code #include <iostream> class Blah { int i; public:
5
1690
by: White Wolf | last post by:
Hi, I would like to double check how long a temporary returned by a function lives? Suppose I have an instance of a class type C, which has a member function returning some sort of wrapper-decorator by value - thereby creating an unnamed temporary. Assuming I start using the temporary (by calling its members) in the same expression, can I assume that it will live long enough to serve those calls?
7
1964
by: Andy Lomax | last post by:
The C99 standard contains various statements like this one (in this case, 6.5.16, assignment operator): >If an attempt is made to modify >the result of an assignment operator or to access it after the next sequence point, the >behavior is undefined. What does this actually mean? Can anyone give me a code example that leads to undefined behaviour?
3
1582
by: Carlo Capelli | last post by:
I found a change in the following code, that behaved correctly in VC++ 6. #include <strstream> using namespace std; void main() { char x; ostrstream(x, 100) << "pippo" << "pluto" << ends; // here x contains "004400C8pluto"
1
2662
by: Paul Drummond | last post by:
Hi, I hope I am not posting in the wrong place! I have posted to the MFC group but thought I might get more of a response here as the question is C++ specific. If I am wrong to post here or violating some rule please let me know and I promise I won't do it again :) I am using MFC/COM _bstr_t() in Visual C++ 6.0 and I am insterested to know what constructors get called when the following call is made:
17
10265
by: Henning Hasemann | last post by:
I have a function which gets the adress of an object as argument. It does some comparsion with the object's contents and then returns. no Reference or pointer to the object is stored or will be used after the function has returned. Say the function whould be named f and the objects class whould be T it'll look like this: bool f(T* thing) { return thing->foobar == 5; // Just a stupid example
2
2390
by: perdubug | last post by:
Somebody told me that Tasking C166 C++ compiler has problems with temporary objects in function call parameters. He gave me below examples: Case 1:Wrong: rc = foo(&bar()); Case 2:Right: bar b; rc=foo(&b);
3
12882
by: daniell | last post by:
/* Triangle.cpp */ // Use a pure virtual function. #include <iostream> #include <cstring> using namespace std;
10
1795
by: subramanian100in | last post by:
Consider the following code: #include <iostream> #include <cstdlib> using namespace std; int main() { const double& ref = 100;
0
8379
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8294
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8709
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8494
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8596
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7309
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.