What does the explicit keyword mean

0 votes
What does the explicit keyword mean in C++?
Jun 27 in C++ by Nicholas
• 5,020 points
14 views

1 answer to this question.

0 votes

To resolve the parameters to a function, the compiler is permitted to do one implicit conversion. 

This implies that the compiler can utilise constructors with a single argument to convert from one type to another to find the correct type for a parameter.

Here's an example class with a constructor that can be used for implicit conversions:

class Foo
{
private:
  int m_foo;

public:
  // single parameter constructor, can be used as an implicit conversion
  Foo (int foo) : m_foo (foo) {}

  int GetFoo () { return m_foo; }
};

Here's a simple function that takes a Foo object:

void DoBar (Foo foo)
{
  int i = foo.GetFoo ();
}

and here's where the DoBar function is called:

int main ()
{
  DoBar (42);
}

The parameter is an int rather than a Foo object. 

However, Foo has a function Object() { [native code] } that accepts an int, which may be used to transform the parameter to the right type.

The compiler may perform this just once for each argument.

By including the explicit keyword to the function Object() { [native code] }, the compiler is prevented from utilising that function Object() { [native code] } for implicit conversions. 

Including it in the preceding class will result in a compiler error at the function call DoBar (42). 

DoBar (Foo (42)) is now required to explicitly request conversion.

You might want to do this to avoid unintentional building, which might mask flaws.

Exaggerated example:

You have a MyString class with a function Object() { [native code] } that creates a string of the specified length. 

You have a function print(const MyString&) and an overload print (char *string), and you call print(3) (when you meant to call print("3"). 

You expect it to display "3," but it instead prints an empty string of length 3.

answered Jun 27 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

What does the C++ standard state the size of int, long type to be?

I'm seeking for specific information on the sizes of basic C++ types.  I understand that it is determined by the architecture (16 bits, 32 bits, or 64 bits) and the compiler. But are there any C++ standards? On a 32-bit architecture, I'm using Visual Studio 2008.  This is what I get: char : 1 byte short : 2 ...READ MORE

Jul 5 in C++ by Nicholas
• 5,020 points
24 views
0 votes
0 answers

What does the C++ standard state the size of int, long type to be?

I'm seeking for specific information on the sizes of basic C++ types.  I understand that it is determined by the architecture (16 bits, 32 bits, or 64 bits) and the compiler. But are there any C++ standards? On a 32-bit architecture, I'm using Visual Studio 2008.  This is what I get: char : 1 byte short : 2 ...READ MORE

Jul 7 in C++ by Nicholas
• 5,020 points
10 views
0 votes
0 answers

What is the C++ function to raise a number to a power?

What's the best way to raise a n ...READ MORE

Jun 1 in C++ by Nicholas
• 5,020 points
26 views
0 votes
1 answer
0 votes
1 answer

What is this weird colon-member (" : ") syntax in the constructor?

Foo(int num): bar(num) In C++, this is known as a Member Initializer List. Simply put, it sets the value of your member bar to num. There is a significant difference between initializing a member with the Member initializer list and assigning a value to it within the function Object() { [native code] } body. When you use the Member initializer list to initialise fields, the constructors are only called once, and the object is constructed and initialised in a single operation. If you use assignment, the fields will be initialised with default constructors and then reassigned with actual values (via the assignment operator). As you can see, there is an extra overhead of creation and assignment in the latter, which may be significant for user defined classes. Cost of Member Initialization =Object ...READ MORE

answered May 27 in Others by Damon
• 4,960 points
43 views
0 votes
1 answer

The Definitive C++ Book Guide and List

For Beginner (includes those without coding experience) Programming: ...READ MORE

answered Jun 6 in C++ by pranav
• 2,580 points
43 views
0 votes
1 answer

Why is "using namespace std;" considered bad practice?

This has nothing to do with performan ...READ MORE

answered Jun 1 in C++ by Damon
• 4,960 points
64 views
0 votes
1 answer

What is a smart pointer and when should I use one?

A smart pointer is similar to a ...READ MORE

answered Jun 2 in C++ by Damon
• 4,960 points
19 views
0 votes
1 answer

why are copy constructors needed and what are the cases where they are very helpful?

A copy constructor is a member function ...READ MORE

answered May 31 in C++ by Damon
• 4,960 points
47 views
0 votes
1 answer

Why does C++ need the scope resolution operator?

No. There is no scope resolution operator ...READ MORE

answered Jun 1 in C++ by Damon
• 4,960 points
30 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP