Topic: Should I be asking questions on the guranteed behaviour of this code...
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/08/14 Raw View
Jim Hyslop wrote:
>
> In article <399208C5.CEAEB570@wizard.net>,
> James Kuyper <kuyper@wizard.net> wrote:
> > Ade Talabi wrote:
> [snip]
> > > PleaseSolve*& myPara = 0;
> >
> > A PleaseSolve* is created by conversion from 0. Then myPara is set to
> > refer to that pointer.
> No, temporaries are only created for class types (12.2/1). ...
12.2/1 does not restrict temporaries to objects of class type. It only
describes what happens when a temporary of class type is created.
Temporaries of non-class types are not only permitted, but in some cases
are mandatory. They're not described in any detail, because non-class
temporaries don't present the interesting complications that temporaries
of class types do.
Take a look again at 8.5.3. The real problem (which I missed), is that
myPara is not const. If it were, the second half of 8.5.3p5 would apply
- whether or not "PleaseSolve*" were replaced with a class type. That
section allows a temporary in one case, and requires one in the other,
without any reference to whether or not the temporary would be of a
class type. There's even an example given in that section:
const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
Because I missed the 'const' issue, I canceled my message as soon as
someone else did point it out. I guess your newsserver didn't honor my
'cancel' request.
...
> This message was posted using plain text only. Any hyperlinks you may
> see are not part of my message, and constitute a violation of copyright.
I think not - the hyperlinks at the bottom of the page are inserted by
the moderators of this newsgroup, and I doubt that they violate any
copyrights.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 2000/08/15 Raw View
James Kuyper <kuyper@wizard.net> wrote:
>Jim Hyslop wrote:
>>
>> In article <399208C5.CEAEB570@wizard.net>,
>> James Kuyper <kuyper@wizard.net> wrote:
>> > Ade Talabi wrote:
>> [snip]
>> > > PleaseSolve*& myPara = 0;
>> >
>> > A PleaseSolve* is created by conversion from 0. Then myPara
>> > is set to
>> > refer to that pointer.
>> No, temporaries are only created for class types (12.2/1). ...
>
>12.2/1 does not restrict temporaries to objects of class type.
>It only
>describes what happens when a temporary of class type is
>created.
My apologies, you are correct.
[snip]
>Take a look again at 8.5.3. The real problem (which I missed),
>is that
>myPara is not const. If it were, the second half of 8.5.3p5
>would apply
>- whether or not "PleaseSolve*" were replaced with a class type.
Yes, that's the real crux of the issue - the non-const temporary.
[snip]
>Because I missed the 'const' issue, I canceled my message as
>soon as
>someone else did point it out. I guess your newsserver didn't
>honor my 'cancel' request.
Deja news doesn't usuall honour the cancel request,
unfortunately.
>> This message was posted using plain text only. Any hyperlinks
>> you may
>> see are not part of my message, and constitute a violation of
>> copyright.
>
>I think not - the hyperlinks at the bottom of the page are
>inserted by
>the moderators of this newsgroup, and I doubt that they violate
>any
>copyrights.
Those are not the hyperlinks I was referring to.
If you browse through some of the web-based news services such as
Deja News, they will insert _in the body of my message_ links to
product names they recognize, such as Visual C++ (go to Deja
News, look up this article and you will see a hyperlink as the
product I just mentioned). By inserting these links, it is my
opinion that Deja has altered the meaning of my posts to suggest
that I endorse these products. I am currently seeking legal
advice as to whether or not it actually does constitute copyright
infringement.
--
Jim
This message was posted in plain text, without any hyperlinks.
Any hyperlinks you see were added by other parties after I posted
this message. I do not endorse any products or services that may
be hyperlinked to this message.
-----------------------------------------------------------
Got questions? Get answers over the phone at Keen.com.
Up to 100 minutes free!
http://www.keen.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: =?ISO-8859-1?Q?J=F6rg?= Barfurth <joerg.barfurth@attglobal.net>
Date: 2000/08/11 Raw View
Hi,
Should I be asking why you (or anybody) would want to know ...
Am 09.08.00, 19:09:38, schrieb Ade Talabi <adetalabi@clara.co.uk> zum Thema
Should I be asking questions on the guranteed behaviour of this code...:
> //Please what is the guaranteed behaviour of this setup.
What do you expect it to do ?
As posted, this should not compile without errors (or at least warnings)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 2000/08/13 Raw View
In article <399208C5.CEAEB570@wizard.net>,
James Kuyper <kuyper@wizard.net> wrote:
> Ade Talabi wrote:
[snip]
> > PleaseSolve*& myPara = 0;
>
> A PleaseSolve* is created by conversion from 0. Then myPara is set to
> refer to that pointer.
No, temporaries are only created for class types (12.2/1). A simple
pointer is not a class type.
8.5.3 requires an object. Since the compiler can't create an object,
the statement is ill-formed.
--
Jim
This message was posted using plain text only. Any hyperlinks you may
see are not part of my message, and constitute a violation of copyright.
I do not endorse any products or services that may be linked to this
message.
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 2000/08/10 Raw View
To answer your subject line: Certainly, ask away - but do pay attention
to the answers. Many people have answered you and said "it shouldn't
compile, because you can't initialize a non-const reference with an
rvalue" to which you replied, "Yeah, but my compiler does it and is it
guaranteed?"
Also, remember that this is not a chat line - do not expect
instantaneous responses. As of the time I write this, you have posted
this exact question three times now: twice in this newsgroup, once in
the unmoderated newsgroup. At this point (August 10 at 9:45 a.m. EST),
at least six different people have responded to your question with an
appropriate answer.
Bottom line: Go ahead and ask. But don't ask repeatedly, and pay
attention to the answers.
[snip 3rd posting of the question]
--
Jim
I ignore all email from recruitment agencies. Except that
I reserve the right to send rude, nasty replies to recruiters.
Please do not send me email with questions - post
here.
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/08/10 Raw View
Ade Talabi wrote:
>
> //Please what is the guaranteed behaviour of this setup.
>
> //forward
> #include<stdio.h>
>
> class PleaseSolve
> {
> public:
> int num;
> };
>
> class SimplePointer
> {
>
> public:
>
> PleaseSolve& SomeSimpleFunc(PleaseSolve*& now);
> int mynow;
>
> };
>
> //impl
>
> PleaseSolve& SimplePointer::SomeSimpleFunc(PleaseSolve*& now)
> {
> int unsolved = 1;
>
> if (unsolved == 1)
> {
> PleaseSolve * myObj = new PleaseSolve();
> myObj->num = 300;
>
> now = myObj;
> }
> return *now;
> }
> main()
> {
>
> SimplePointer myPointer;
Automatically allocates memory for a SimplePointer. Then calls the
implicitly declared and defined constructor for a SimplePointer, which
in turn calls the implicitly declared and defined constructor for
myPointer.mynow, which does nothing. Since this is all public, any
decent compiler will optimize away the function calls.
> PleaseSolve*& myPara = 0;
A PleaseSolve* is created by conversion from 0. Then myPara is set to
refer to that pointer.
> PleaseSolve gone = myPointer.SomeSimpleFunc(myPara);
SomeSimpleFunc() sets the pointer referred to by myPara to point to a
new PleaseSolve object, initialized with a value of 300 in num, and
returns a reference to that object.
The implicitly declared and defined PleaseSolve copy constructor is used
to copy the new PleaseSolve object into 'gone', with the result that
gone.num = 300.
> fprintf( stdout, "my now is %d\n", gone.num );
The text "my now is 300\n" shows up on the standard output. The
implicitly declared and defined SimplePointer destructor is called on
myPointer, which does nothing. Then the memory for myPointer is
released.
> }
At this point, you've forgotten to delete the dynamically allocated
object pointed at by the pointer referred to by myPara, so you've got a
memory leak. Also, the mynow member of SimplePointer is completely
unused in this code.
At least that's my take on it. I wish you'd asked your question more
directly, whatever it was. For instance, you could have told us what you
think should happen, or what you wanted to happen, and then told us what
actually did happen. Then you could ask whether you or the compiler was
correct.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ade Talabi <adetalabi@clara.co.uk>
Date: 2000/08/09 Raw View
//Please what is the guaranteed behaviour of this setup.
//forward
#include<stdio.h>
class PleaseSolve
{
public:
int num;
};
class SimplePointer
{
public:
PleaseSolve& SomeSimpleFunc(PleaseSolve*& now);
int mynow;
};
//impl
PleaseSolve& SimplePointer::SomeSimpleFunc(PleaseSolve*& now)
{
int unsolved = 1;
if (unsolved == 1)
{
PleaseSolve * myObj = new PleaseSolve();
myObj->num = 300;
now = myObj;
}
return *now;
}
main()
{
SimplePointer myPointer;
PleaseSolve*& myPara = 0;
PleaseSolve gone = myPointer.SomeSimpleFunc(myPara);
fprintf( stdout, "my now is %d\n", gone.num );
}
--
/--------------------------------------------------------------\
| Ade Talabi | Internet : adetalabi@clara.co.uk |
| ::M1ETW:: | Web Page : http://www.net-africa.com |
\--------------------------------------------------------------/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]