Topic: Extension or standard?
Author: eteran <evan.teran@gmail.com>
Date: Mon, 10 May 2010 23:42:26 CST Raw View
To follow up on what Ian said, to make the code correct, you simply
need to change the function to take a const reference like this:
void f(const A &rA) { ... }
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Marco Nef" <maillist@shima.ch>
Date: Mon, 3 May 2010 13:07:42 CST Raw View
Given the following snipplet:
class A
{
};
class B : public A
{
public:
B(int iVal) {}
};
void f(A &rA) { ... }
Following the standard, is it allowed to do the following call?
f(B(10));
I thought it were, but my compiler (MS VC++ 2008) keeps saying that it uses
a nonstandard extension for the conversion from "B" to "A&" (remark: I'm
about to switch to warning level 4).
- Marco
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Ian Collins <ian-news@hotmail.com>
Date: Mon, 3 May 2010 18:02:35 CST Raw View
On 05/ 4/10 07:07 AM, Marco Nef wrote:
> Given the following snipplet:
>
> class A
> {
> };
> class B : public A
> {
> public:
> B(int iVal) {}
> };
>
> void f(A &rA) { ... }
>
> Following the standard, is it allowed to do the following call?
>
> f(B(10));
>
> I thought it were, but my compiler (MS VC++ 2008) keeps saying that it uses
> a nonstandard extension for the conversion from "B" to "A&" (remark: I'm
> about to switch to warning level 4).
It isn't following the standard. You are initialising a non-const
reference with a temporary.
--
Ian Collins
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu <std-c%2B%2B@netlab.cs.rpi.edu>]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]