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

about temporary object

cxf
class A{
public:
A()
{
func(0);
};
virtual void func(int data)
{
printf("A1 :%d\n",data);
}
virtual void func(int data) const
{
printf("A2 :%d\n",data);
}
};

A().func(1) invoke which function? ,why?
I think it is " void func(int data) const", because A() return a
temporary object, which should be const. But when it is executed with
vc 6.0, it invokes "void func(int data)"!

please help me, thank you!
Aug 3 '08 #1
4 1608
On Aug 3, 1:00*pm, cxf <cxf....@gmail.comwrote:
class A{
public:
* * * * A()
* * * * * * * * {
* * * * * * * * * * * *func(0);
* * * * * * * * *};
* * * * virtual void func(int data)
* * * * * * * * {
* * * * * * * * * * * * printf("A1 :%d\n",data);
* * * * * * * * }
* * * * virtual void func(int data) const
* * * * * * * * {
* * * * * * * * * * * *printf("A2 :%d\n",data);
* * * * * * * * }

};

A().func(1) invoke which function? ,why?
I think it is " void func(int data) const", because A() return a
temporary object, which should be const. But when it is executed with
vc 6.0, it invokes "void func(int data)"!

please help me, thank you!
The line:

A().func(1);

Means 2 invocations of "func". The first one is going to be the non-
const one, because its called from inside the constructor. The
constructor is suppose to modify the instance, therefore it calls the
non-const one.

As for the second "func" call, its again the non-const one, for
obvious reasons: "A()" returns an A, not a "const A".

Observe:
A().func(1);
Output:
A1 :0
A1 :1

Now:
const A* a = new A(); // "a" is now declared const
a->func(1);
Output:
A1 :0
A2 :1

However:
A* a = new A(); // "a" NOT declared const, defaults to non-const
a->func(1);
So obviously:
A1 :0
A1 :1
Aug 3 '08 #2
On Aug 3, 1:00*pm, cxf <cxf....@gmail.comwrote:
I think
This is where everything went wrong... :P
Half a minute alone with a compiler always gets the last say though.
Aug 3 '08 #3
On Aug 3, 6:00 am, cxf <cxf....@gmail.comwrote:
class A{
public:
A()
{
func(0);
};
virtual void func(int data)
{
printf("A1 :%d\n",data);
}
virtual void func(int data) const
{
printf("A2 :%d\n",data);
}

};
A().func(1) invoke which function?
The non-const one.
,why?
Because it can. Literally: if all other things are equal, and
two functions differ only by their const-ness, the compiler will
prefer the non-const version whenever it can be called.
I think it is " void func(int data) const", because A() return
a temporary object, which should be const.
Why? You asked the compiler to construct an A, not an A const.

There is no way to request an A const directly, but you can
return one:

extern A f() ;
extern A const g() ;

f().func() ; // invokes non-const function
g().func() ; // invokes const function.
But when it is executed with vc 6.0, it invokes "void func(int
data)"!
As required.

The issues are not trivial, and largely historically
conditionned. But basically, a temporary is something called an
rvalue: if it has non-class type, it has no cv-qualifiers in its
type (but what you can do with it is restricted by the
constraints concerning lvalue and rvalue of the different
operators). If the rvalue has class type, however, things
become more complicated, because const-ness (and volatile-ness)
affect what you can do with it. Since "int f()" returns an int
(and not an int const), "A f()" returns an "A", and not an "A
const". However, while changing the declaration to "int const
f()" does nothing but create confusion in the minds of the
reader (because the return value is an rvalue of non-class type,
and isn't cv-qualified), changing it to "A const f()" does have
an effect (because despite being an rvalue, the temporary must
still be a real object, with address, etc., and there are ways
of getting that address).

Finally, of course, the situation is additionally complicated
because of the rule that you cannot use an rvalue (even if it
has class type) to initialize a non-const reference, but you can
use one (even if it doesn't have class type, and isn't an
"object") to initialize a const reference (which means that,
indirectly, you can get the address of the temporary).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 3 '08 #4
cxf wrote:
class A{
public:
A()
{
func(0);
};
virtual void func(int data)
{
printf("A1 :%d\n",data);
}
virtual void func(int data) const
{
printf("A2 :%d\n",data);
}
};

A().func(1) invoke which function? ,why?
The non-const version, because A() is not const.
I think it is " void func(int data) const", because A() return a
temporary object, which should be const.
You must be mixing that up with binding a reference to a temporary. In your
case, no reference is involved, so it doesn't apply here.

Aug 3 '08 #5

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
4
by: Tran Tuan Anh | last post by:
Dear all, I am new in C++, and now get confused about a lot of things. I wrote this simple code to test the vector. class Temp { public: int x; }; int main() { vector<Temp> v;
5
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...
3
by: Tony Johansson | last post by:
Hello experts! I have this piece of code. No user defined copy constructor exist. AccountForStudent create(long number) { AccountForStudent local(number, 0.0); return local; } int main() {
2
by: flamexx7 | last post by:
http://www.rafb.net/paste/results/V3TZeb28.html In this code, why copy constructor is not called while returning object from no_arg() . I was trying to find answer in C++ Standard. and there it's...
15
by: Lighter | last post by:
I find a BIG bug of VS 2005 about string class! #include <iostream> #include <string> using namespace std; string GetStr() { return string("Hello");
4
by: gg9h0st | last post by:
i worte a simple code below. ------------------------------------------------------------------------------------ #include "stdafx.h" class Object { public: int a;
15
by: Jeroen | last post by:
Hi all, I've got a very specific question about the evaluation order in C++. Assume some kind of custom array class, with an overloaded subscript operator. In the following code: { my_array...
10
by: Jess | last post by:
Hello, If I create a temporary object using a dynamically created object's pointer, then when the temporary object is destroyed, will the dynamically created object be destroyed too? My guess...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.