explicit keyword

B

Bart Simpson

When is this used in constructor declarations?
Why is it used?

example :

class MyClass
{
public:
explicit MyClass(const int num);
...
}

I have seen many different examples and different explantions ... ???

Anyone care to summarize what its used for (something about preventing
implicit casts) ?
 
R

Ron Natalie

Bart Simpson wrote:

Anyone care to summarize what its used for (something about preventing
implicit casts) ?

Implicit conversions. A cast is an explicit conversion.

A constructor that can be called with one argument is called a
converting constructor. This means that you will get implicit
conversions from the type of the argument to the type of the
constructor. For example:

class MyString {
public:
MyString(const char* arg);
//...
};

This will convert const char* to my string and that's probably what
you want, you can then do things like:

void func(MyString);

func("This will be converted");

However, sometimes you don't want the conversion to happen implicitly.
For example, lets say we have an alternate constructor that takes a
size:

MyString(int max_length);

Now you probably don't want this implicitly converted:

func(5);

makes little sense to turn wayward numbers into your
string type (interpretting as a lenth). To stop this
you say:

explit MyString(int max_length);

The compiler will reject
func(5);
but will take

func(MyString(5)); // I mean this!
 
V

Victor Bazarov

Bart said:
When is this used in constructor declarations?

When you need to avoid using that constructor in implicit
conversion sequences.
Why is it used?

Because that's the only way to prevent implicit conversions and
yet allow parameterised construction.
example :

class MyClass
{
public:
explicit MyClass(const int num);
...
}

I have seen many different examples and different explantions ... ???

Anyone care to summarize what its used for (something about preventing
implicit casts) ?

I'll give you an example. If you have a function taking 'MyClass'
argument:

void foo(MyClass);

you won't be able to call it with an int:

foo(42); // error

because constructing of 'MyClass' from an int implicitly is impossible.
To call 'foo' you'd have to spell it out for the compiler:

foo(MyClass(42));

* * *

In most cases, if you have to ask what a certain construct is for, you
most likely don't need it. When you will have the need to do something,
ask "how to do it", and you will find a way. I recommend agains
clogging your brain with information you're not likely to need. Just
a thought...

V
 
B

blangela

When you need to avoid using that constructor in implicit
conversion sequences.


Because that's the only way to prevent implicit conversions and
yet allow parameterised construction.







I'll give you an example. If you have a function taking 'MyClass'
argument:

void foo(MyClass);

you won't be able to call it with an int:

foo(42); // error

because constructing of 'MyClass' from an int implicitly is impossible.
To call 'foo' you'd have to spell it out for the compiler:

foo(MyClass(42));

* * *

In most cases, if you have to ask what a certain construct is for, you
most likely don't need it. When you will have the need to do something,
ask "how to do it", and you will find a way. I recommend agains
clogging your brain with information you're not likely to need. Just
a thought...

V

In the example above provided by Victor, the following will also work
with the explicit ctor

foo(static_cast<Myclass> (42)); // C++ cast

or

foo((Myclass) 42); // old C style cast


All 3 invoke the same conversion ctor explicitly.
 

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,294
Messages
2,571,511
Members
48,198
Latest member
couriermedicine

Latest Threads

Top