What is the 'explicit' mean as a key word?

S

sun1991

Hi all !
What is the 'explicit' mean as a key word? Thank you!

Means while the comiler see:
SomeClass s = "abc";

it won't borther to translate it into:
SomeClass s("abc");
(if possible, and will simply fail you instead)
 
A

Alf P. Steinbach

* dolphin:
Hi all !
What is the 'explicit' mean as a key word? Thank you!

Currently it applies to constructors only. A class T constructor of one
argument can by default be called implicitly to convert the argument to
type T. If it's declared "explicit" it cannot be called implicitly: you
have to call it explicitly, writing T(v).

Much of the reason is the lack of named actual arguments in C++. E.g.,
a constructor of one int argument is only seldom meant as a conversion
from int to T: it doesn't produce a T object that in any practical sense
"corresponds" to the int value, yet its usage is the same as a
conversion, a mapping from int to T. Instead of using "explicit" to
avoid implicit conversion you can use any of a number of workarounds for
the lack of named actual arguments, e.g. see the FAQ item titled "What
is the "Named Parameter Idiom"?", currently available at e.g. <url:
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.18>.

Possibly the "explicit" syntax will be extended also to operator U()
conversion functions.
 
N

Neelesh Bodas

Hi all !
What is the 'explicit' mean as a key word? Thank you!

'explicit' keyword is used with a unary constructor to indicate that
the unary constructor *cannot* act as a conversion constructor.

struct X
{
X(int);
};

struct Y
{
explicit Y(int);
};

void foo()
{
X s = 5; //works, since X::X(int) acts as conversion constructor
Y p = 5; //Error, since Y::Y(int) cannot act as conversion
constructor.
}

-N
 
D

dolphin

* dolphin:


Currently it applies to constructors only. A class T constructor of one
argument can by default be called implicitly to convert the argument to
type T. If it's declared "explicit" it cannot be called implicitly: you
have to call it explicitly, writing T(v).

Much of the reason is the lack of named actual arguments in C++. E.g.,
a constructor of one int argument is only seldom meant as a conversion
from int to T: it doesn't produce a T object that in any practical sense
"corresponds" to the int value, yet its usage is the same as a
conversion, a mapping from int to T. Instead of using "explicit" to
avoid implicit conversion you can use any of a number of workarounds for
the lack of named actual arguments, e.g. see the FAQ item titled "What
is the "Named Parameter Idiom"?", currently available at e.g. <url:http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.18>.

Possibly the "explicit" syntax will be extended also to operator U()
conversion functions.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

How to use it? And why use it and where use it?
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

How to use it? And why use it and where use it?


Consider this code:

#include <iostream>

class Foo
{
int val;
public:
Foo(int i) : val(i) {}
};

void bar(Foo f) { }

int main()
{
bar(1);
}

In main() we call bar(), but there's no version of bar() that takes an
int as an argument. However a Foo object can be constructed from an int,
so what happens is that first a Foo object is created and then bar() is
called. If you add explicit in front of Foo's constructor the code will
no longer compiler since the Foo object can no longer implicitly be
created from the integer.
 

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

Forum statistics

Threads
474,291
Messages
2,571,455
Members
48,132
Latest member
KatlynC08

Latest Threads

Top