explicit keyword

G

George2

Hello everyone,


I am reading the C++ Programming Language book, but can not find the
function of explicit keyword of constructor. Could anyone explain its
usage or refer some learning materials please?


thanks in advance,
George
 
V

Victor Bazarov

George2 said:
I am reading the C++ Programming Language book, but can not find the
function of explicit keyword of constructor. Could anyone explain its
usage or refer some learning materials please?

Have you tried googling?

V
 
A

Alf P. Steinbach

* Victor Bazarov:
Have you tried googling?

One problem with googling is that the FAQ confuses explicit constructor
calls with placement new syntax.

Short of it, "explicit" on a converting constructor suppresses all
implicit calls of that constructor, so that all calls must be explicit.

This helps avoid silly automatic conversions such as the compiler
accepting assignment of integer 666 to myVector, treating it as myVector
= std::vector<SomeType>(666) -- if you want that, you'll have to write
it explicitly, as shown here. Disclaimer: I haven't checked whether
that std::vector constructor is "explicit". I assume it is.

Cheers, & hth.,

- Alf
 
D

Dave Rahardja

George2 said:
Hello everyone,


I am reading the C++ Programming Language book, but can not find the
function of explicit keyword of constructor. Could anyone explain its
usage or refer some learning materials please?


thanks in advance,
George

The "explicit" keyword is defined in 12.3.1:

"An explicit constructor constructs objects just like non-explicit
constructors, but does so only where the
direct-initialization syntax (8.5) or where casts (5.2.9, 5.4) are
explicitly used."

In other words, implicit conversion of the constructor's parameter is
turned off when a constructor is declared explicit.

-dr
 
D

David Harmon

On Mon, 10 Dec 2007 22:00:36 -0800 (PST) in comp.lang.c++, George2
I am reading the C++ Programming Language book, but can not find the
function of explicit keyword of constructor.

Section 11.7.1
 
I

Ian Collins

George2 said:
Hello everyone,


I am reading the C++ Programming Language book, but can not find the
function of explicit keyword of constructor.

Have you tried the index?
 

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
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top