473,396 Members | 1,966 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,396 software developers and data experts.

temporaries

Hello all,
Please consider this code :

class X {
int x;
public:
X(int p ) : x(p) { }
operator int() { x = 1; return x; }
};

X foo(int i)
{
return X(i);
}
int main()
{
int u = 100;
int p = foo(u);
}

What I understand here (Please correct if I am wrong) :
The function foo takes an integer and returns a value of type X (this
is 'return by value'). The return value of foo is stored in some
temporary. This value is then converted to an integer, using operator
int() and then assignment takes place.

What I donot understand is that :
temporaries are always constant. On the other hand, operator int() is
not a const function (rather, it cannot be a const function, it
modifies the data member x). Connsequently, this code should fail to
compile. (How can operator int() be called on a const object ? )

But thats not the case. This is a valid C++ code.
Any explanation will be useful. Thanks.

Nov 22 '05 #1
1 1392
Neelesh Bodas wrote:
Hello all,
Please consider this code :

class X {
int x;
public:
X(int p ) : x(p) { }
operator int() { x = 1; return x; }
};

X foo(int i)
{
return X(i);
}
int main()
{
int u = 100;
int p = foo(u);
}

What I understand here (Please correct if I am wrong) :
The function foo takes an integer and returns a value of type X (this
is 'return by value'). The return value of foo is stored in some
temporary. This value is then converted to an integer, using operator
int() and then assignment takes place.
The return value may be stored in a temporary. But since foo always
returns the same object, the compiler may optimize the temporary away.
But doing so would not affect the correctness or the outward behavior
of the program.
What I donot understand is that :
temporaries are always constant. On the other hand, operator int() is
not a const function (rather, it cannot be a const function, it
modifies the data member x). Connsequently, this code should fail to
compile. (How can operator int() be called on a const object ? )
X's operator() int can be called on a non-const object because it is a
non-const method - which is the only requirement that must be met to
invoke operator int(). There is no rule that states that temporaries
must be constant. True, the compiler will not bind a temporary to a
reference passed as a parameter unless the reference is declared const;
but that rule is not at all the equivalent to a rule that temporaries
themselves must always be const. And in fact there is no such rule.
But thats not the case. This is a valid C++ code.


There is no obvious reason to expect this program not to work.

Greg

Nov 22 '05 #2

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

Similar topics

1
by: kaede | last post by:
Hi all, I would like to know if the following code is valid and not ill-formed. Data getData() { return Data(1, 2, 3); }
13
by: John Harrison | last post by:
Rather a long program I'm afraid but I don't think I can cut it down any further. What I'm trying to do is construct a complex object Y from several X objects in a complex expression. I'm trying...
10
by: JKop | last post by:
Firstly, we all know that temporaries are non-const, which I shall demonstrate with the following code: struct Blah { int monkey; void SetMonkey(int const supplied) { monkey = supplied;
5
by: Dave | last post by:
Hello all, I've been wondering... Why is it that a reference may be bound only to a const object? If a reference were bound to a non-const object and that object were modified, what harm could...
3
by: bb | last post by:
Hi, Have a query regarding the life of temporaries. Here is the code... class MyNumber { public: MyNumber(int n) : n(n) { cout << "Object Constructed." << endl; }
7
by: REH | last post by:
Though unsafe, is this legal: const int& foo(const int& i) { return i; } int j = foo(5); does the temporary live long enough to be assigned to j?
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
6
by: wizwx | last post by:
Is there anything wrong with the following code? class A { ... }; class B : public A { ... }; // definitions of class A and B, these are OK Foo() { A & a = B(); // ?? A * p = &B(); // ??...
13
by: dragoncoder | last post by:
Hi everyone, please consider the following function:- const int& foo ( const double& d ) { return d; } g++ compiles it with warnings and solaris CC gives error. I want to know if the code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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.