473,769 Members | 7,388 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 1555
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.d udewrote:
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.c omwrote:
On Feb 7, 4:20 pm, red floyd <no.s...@here.d udewrote:


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.d udewrote:
>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
1973
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 interface, or should I make it implicit that a pointer is being added to the list, like so 2. void add( foo *f ) { lst.push_back( f ); }
3
2284
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
1845
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 references going by the const guidelines. Taking that into consideration, how does one get a constant reference to a pointer. class A
7
1419
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 a pointer to a type pointer, T*** is a pointer to type pointer to type pointer and so on? 2) Is there any history behind the use of the "*" operator in providing dereferencing capabilities or is it arbitrary? How did B handle pointers? (if it...
13
2102
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: SmrtPtr.hpp #pragma once
7
1683
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 think when the result of a function is returned, then a non-temporary object is created, with value copied from the local temporary object. If the
2
1823
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
1911
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 some light on this! (code abbreviated for clarity) I have a parent class, DetailCollection, with a child class KeycodeTracker:
5
2650
by: coolguyaroundyou | last post by:
Consider the following codes: class abc { int i; public: abc() : i(0) {} void func() { .....some code....} };
0
9589
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
9423
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
10216
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
10049
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
9997
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
9865
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
5310
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.