gcc bug ?
Phil Edwards
pedwards@disaster.jaj.com
Sun Nov 12 17:22:00 GMT 2000
On Mon, Nov 13, 2000 at 11:47:48AM +1100, LOOK, Peter wrote:
>
> Enclosed please find a simple program eg3_2_1.cpp which is an example in
> P.96 of "Teach Yourself C++" 3rd Edition by Herbert Schildt.
As a side note, I feel ethically compelled to mention that Schildt's books
are considered to be of poor quality by many members of the C++ community.
If you want a "teach yourself" book, you might try one by Jesse Liberty.
> But, I use gcc to compile. It WORKS, no complaint. When I run the execution
> file. It works fine. It should be failed to compile because wrong type
> matching !! Is it a bug ?
No. There is no compiler bug, and the code is valid C++. You are seeing
the results of implicit type conversion, via conversion constructors.
What happens is that the language allows an int to be implicitly converted
into a samp by way of the single-argument constructor, samp::samp(int).
When you try passing an int to sqr_it, the compiler constructs a temporary
samp object using the constructor.
Change the constructor to read
samp (int n) { cout << "here I am\n"; i = n' }
and you will see it being called.
You can prevent implicit constructor conversion by using the 'explicit'
keyword in front of those kinds of constructors.
Also, as another side note:
> **********************************************************************
> This e-mail message (along with any attachments) is intended only for
> the named addressee and may contain information that is confidential
> or privileged. If you are not the intended recipient you are hereby
> notified that any dissemination, copying or use of any of the
> information is prohibited. If you have received this e-mail message
> in error, please notify us immediately by return e-mail and delete all
> copies of the original message and attachments.
> **********************************************************************
That sort of thing is considered rather poor form on a public,
publicly-archived mailing list. Just FYI, nothing personal.
Phil
--
pedwards at disaster dot jaj dot com | pme at sources dot redhat dot com
devphil at several other less interesting addresses in various dot domains
The gods do not protect fools. Fools are protected by more capable fools.
More information about the Gcc-bugs
mailing list