Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > about temporary object

Reply
Thread Tools

about temporary object

 
 
cxf
Guest
Posts: n/a
 
      08-03-2008
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!
 
Reply With Quote
 
 
 
 
raicuandi
Guest
Posts: n/a
 
      08-03-2008
On Aug 3, 1:00*pm, cxf <(E-Mail Removed)> 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?
> 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
 
Reply With Quote
 
 
 
 
raicuandi
Guest
Posts: n/a
 
      08-03-2008
On Aug 3, 1:00*pm, cxf <(E-Mail Removed)> wrote:
> I think


This is where everything went wrong...
Half a minute alone with a compiler always gets the last say though.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-03-2008
On Aug 3, 6:00 am, cxf <(E-Mail Removed)> 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?


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:(E-Mail Removed)
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
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      08-03-2008
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
anonymous array of strings // ("taking address of temporary"- how long is temporary valid?) anon.asdf@gmail.com C++ 7 02-12-2008 10:58 AM
creating a temporary object using dynamically created object Jess C++ 10 05-18-2007 02:21 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
returning copy of the temporary string object qazmlp C++ 3 03-07-2004 04:57 PM
A reference to non-const to be bound to a temporary object John Ky C++ 9 02-23-2004 12:53 AM



Advertisments