What is the output of this program? and why?

R

Richard Herring

[snip]
I read the thread after I tried to solve it I confess.

Not in the context you created. Here is the error from comeau:

error: "CMyBase::CMyBase(CMyBase &)", required for copy that was
eliminated, is not callable because reference parameter cannot be bound
to rvalue

And from gcc:

error: no matching function for call to `CMyBase::CMyBase(CMyBase)'
candidates are: CMyBase::CMyBase(int)
CMyBase::CMyBase(CMyBase&)
It is a silly piece of code I must agree. Designed to be difficult to
dechipher.
And I bow to all you guys obviously superior knowledge. I'm humbled.
:)

What is the borland compiler doing wrong here then?

Not diagnosing an attempt to bind a temporary to a non-const reference.
(since I compiled
it and ran it just fine)

All kinds of programs with undefined behaviour run "just fine".
Should I begin to doubt all programs I have made for it?
No.

(I know about borlands herasy with the standard on acount of the vcl.

Heresy? They have added a couple of keywords and some libraries, and
some of the added library classes have non-C++ construction and
destruction semantics because they aren't written in C++. They haven't
altered the semantics of any standard constructs. AFAIK in other
respects the compiler follows the standard reasonably well.
And I really am two minds about the additions they have made. On the
one hand its certainly more readable, but on the other its less
portable.

That need not be a problem. If you want portability, just don't use the
extensions. (My personal preference would be to keep my code as close to
the standard as possible, with just a thin layer of extensions where
it's necessary to interface to their GUI API. But we're drifting off
topic here.)
But have they made some dangerous stuff to core
functionality?)

No. (Well, no compiler is perfect, but there is no reason to worry about
this one just because it supports some extensions.)
 
H

Howard

Alf P. Steinbach said:
* (e-mail address removed):
[forgot to include the question]

One must presume you have a compiler at hand (that this is not HOMEWORK by
some less than competent lecturer), and so that the question is meant to
be what the output should be according to the standard.

I suspect it IS homework, given the question (posed in the subject line).
Who writes a program and then asks "what is the output? and why?"...? (Also
notice no response from the OP since posting. Another sign of "do my
homework for me" syndrome.)

-Howard
 
K

kalamantina

Genltemen, I really appreciate all your help, believe me its not
homework,
I used C++ back in the day and I moved on to other .NET languages,
this question is something that appeared on an interview, and I still
haven't gotten an answer yet for the compile yet.
but I am sure that programm compiles.
 
D

Daniel T.

Genltemen, I really appreciate all your help, believe me its not
homework,
I used C++ back in the day and I moved on to other .NET languages,
this question is something that appeared on an interview, and I still
haven't gotten an answer yet for the compile yet.
but I am sure that programm compiles.

It may compile on some system that ignore the standard yes, but that
doesn't mean it's legal C++ code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,297
Latest member
EngineerD

Latest Threads

Top