473,505 Members | 13,925 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question regarding pointer/reference to temporaries

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(); // ??
.......
}

I guess a and p are referencing temporaries returned by B(). Is this
the case? Anything wrong?

Feb 7 '07 #1
6 1540
wizwx wrote:
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(); // ??
.......
}

I guess a and p are referencing temporaries returned by B(). Is this
the case? Anything wrong?
Make a const, and there's not problem with it. I don't believe it's
valid as written, but can't find chapter&verse to back me up.

I'm not sure if the language allows the second one, but again, I think
it's bad.

Comeau complains about both. Noting that the first case you need an
lvalue on the right. The second, "expression must be an lvalue or
function designator". Since B() is an rvalue, that would do it.

Feb 7 '07 #2
wizwx wrote:
Is there anything wrong with the following code?
class A { ... };
class B : public A { ... }; // definitions of class A and B, these are
OK

Foo() {
Ill-formed: missing return type. Implicit return types are not allowed in C++.
A & a = B(); // ??
Ill-formed: a non-constant reference cannot be bound to a temporary object.
A * p = &B(); // ??
Ill-formed (assuming that the built-in unary '&' is used): address-of operator
cannot be applied to temporary object since it is not an lvalue.
.......
}

I guess a and p are referencing temporaries returned by B(). Is this
the case? Anything wrong?
The code is ill-formed, which normally means that it cannot be compiled.

--
Best regards,
Andrey Tarasevich
Feb 7 '07 #3
On Feb 7, 4:20 pm, red floyd <no.s...@here.dudewrote:
wizwx wrote:
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(); // ??
.......
}
I guess a and p are referencing temporaries returned by B(). Is this
the case? Anything wrong?

Make a const, and there's not problem with it. I don't believe it's
valid as written, but can't find chapter&verse to back me up.

I'm not sure if the language allows the second one, but again, I think
it's bad.

Comeau complains about both. Noting that the first case you need an
lvalue on the right. The second, "expression must be an lvalue or
function designator". Since B() is an rvalue, that would do it.
Thank you for the reply.
First I would like to clarify, that the codes were from an interview,
so no one would really write crappy code like this. But I know there
is something wrong, but I would like to find it out.

I tested the codes in VC6.0. No error message was issued, nor was
there any warning message. But I believe that you are right in that
the reference/pointer should be const, as temporaries cannot be
lvalue.

According to VC6.0,
A & a = B();
'a' would then reference to an (temporary) object. It works just fine
as if 'a' was declared as an object of class B. 'a' is destructed when
the scope closes.

However, for A * b = &B();
the destructor was invoked soon after the line was executed, which
means that 'b' points to a memory while the object no longer exists.

wandering if the lines of codes has compiler-dependent results.

Feb 7 '07 #4
On Feb 7, 5:48 pm, "wizwx" <wiz...@gmail.comwrote:
On Feb 7, 4:20 pm, red floyd <no.s...@here.dudewrote:


wizwx wrote:
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(); // ??
.......
}
I guess a and p are referencing temporaries returned by B(). Is this
the case? Anything wrong?
Make a const, and there's not problem with it. I don't believe it's
valid as written, but can't find chapter&verse to back me up.
I'm not sure if the language allows the second one, but again, I think
it's bad.
Comeau complains about both. Noting that the first case you need an
lvalue on the right. The second, "expression must be an lvalue or
function designator". Since B() is an rvalue, that would do it.

Thank you for the reply.
First I would like to clarify, that the codes were from an interview,
so no one would really write crappy code like this. But I know there
is something wrong, but I would like to find it out.

I tested the codes in VC6.0. No error message was issued, nor was
there any warning message. But I believe that you are right in that
the reference/pointer should be const, as temporaries cannot be
lvalue.

According to VC6.0,
A & a = B();
'a' would then reference to an (temporary) object. It works just fine
as if 'a' was declared as an object of class B. 'a' is destructed when
the scope closes.

However, for A * b = &B();
the destructor was invoked soon after the line was executed, which
means that 'b' points to a memory while the object no longer exists.

wandering if the lines of codes has compiler-dependent results.- Hide quoted text -

- Show quoted text -
Used g++ to test the code. The first was complained, as the reference
should be const. The second got a warning message.

Feb 7 '07 #5
wizwx wrote:
...
According to VC6.0,
A & a = B();
'a' would then reference to an (temporary) object. It works just fine
as if 'a' was declared as an object of class B. 'a' is destructed when
the scope closes.

However, for A * b = &B();
the destructor was invoked soon after the line was executed, which
means that 'b' points to a memory while the object no longer exists.
...
Both declarations are accepted by VC 6 because of an MS-specific compiler
extension, allowing it to tie pointers and non-constant references to
temporaries. The extensions can be disabled with a /z switch, making VC 6 to
reject this code completely.

From the language point of view, once again, both declarations are ill-formed,
invalid, uncompilable. Any questions about destructors etc. are irrelevant and
make no sense.

If you want to research the MS-specific behavior of VC 6 with extensions, you
need to ask these questions in VC 6 specific newsgroup, since this has nothing
to do with C++ language itself.

--
Best regards,
Andrey Tarasevich
Feb 7 '07 #6
wizwx wrote:
On Feb 7, 4:20 pm, red floyd <no.s...@here.dudewrote:
>wizwx wrote:
>>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(); // ??
.......
}
I guess a and p are referencing temporaries returned by B(). Is this
the case? Anything wrong?
Make a const, and there's not problem with it. I don't believe it's
valid as written, but can't find chapter&verse to back me up.

I'm not sure if the language allows the second one, but again, I think
it's bad.

Comeau complains about both. Noting that the first case you need an
lvalue on the right. The second, "expression must be an lvalue or
function designator". Since B() is an rvalue, that would do it.

I tested the codes in VC6.0. No error message was issued, nor was
there any warning message. But I believe that you are right in that
the reference/pointer should be const, as temporaries cannot be
lvalue.
VC6 predates the standard and has improper behavior regarding this.
VC7.1 has the incorrect behavior by default "as an extension", but it
can be turned off.
Feb 7 '07 #7

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

Similar topics

2
1956
by: Tim Partridge | last post by:
If I have an "add" method that adds a pointer to a foo to a private std::list, should I define add like 1. void add( const foo &f ) { lst.push_back( &f ); } to keep pointers out of my...
3
2269
by: kaede | last post by:
Hi all, Consider the following code fragment: // some data structure class Data { ... } // Container for the data structure Class Container {
6
1829
by: amparikh | last post by:
I know this is something fundamental and I ought to have known it, but somehow this seems to be confusing me a lot. Fundamentally, rvalues and/or temporaries can be bound only to constant...
7
1403
by: rs | last post by:
Just out of idle curiosity: 1)Regarding pointers; I don't have the standard/grammar with me, but T* for type T mean pointer-to-T. Does that mean that with a declaration of type T** is actually...
13
2067
by: Protoman | last post by:
Here's a non intrusive reference counting smart pointer class I'm working on; I keep getting a "22 C:\Dev-Cpp\SmrtPtr.hpp ISO C++ forbids declaration of `SmrtPtrDB' with no type" error. Code: ...
7
1665
by: Jess | last post by:
Hello, I learned that if we do "v.end()", then the returned iterator is a temporary object and hence cannot be changed like --v.end(); Why is the returned iterator a temporary pointer? I...
2
1798
by: Renji Panicker | last post by:
I have a question regarding references. Consider the following code: <code> #include <iostream> class A { int _a1; int _a2; int _a3; };
30
1873
by: Logos | last post by:
I have what may be a bug, or may be a misunderstanding on how pass by reference and class inheritance works in PHP. Since I'm relatively new to PHP, I'm hoping for a little outside help to shed...
5
2626
by: coolguyaroundyou | last post by:
Consider the following codes: class abc { int i; public: abc() : i(0) {} void func() { .....some code....} };
0
7213
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
7098
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...
1
7017
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
7471
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...
1
5026
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...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
406
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.