473,473 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

The use of explicit with constructor

Dear all,

Can experienced users clarify the use of explicit keyword for ctors
with some examples?

Regards,

Jun 7 '06 #1
2 1563

utab wrote:
Dear all,

Can experienced users clarify the use of explicit keyword for ctors
with some examples?

Regards,


class MagicInt
{
int x;
public:
MagicInt(int i) : x(i) {}
};

class ExplicitMagicInt
{
int x;
public:
explicit ExplicitMagicInt(int i) : x (i) {}
};

int main(void)
{
MagicInt i1(5);
ExplicitMagicInt i2(5);
MagicInt i3 = 5;
ExplicitMagicInt i4 = 5;

return 0;
}

Jun 7 '06 #2
utab wrote:
Dear all,

Can experienced users clarify the use of explicit keyword for ctors
with some examples?

Regards,


class A
{
public:
A(int x) : x_(x)
{}

int get_x() { return x_ ; }
private:
int x_ ;
} ;

int f(A a)
{
return a.get_x() ;
}

int main()
{
// Fine, class A can be implicitly constructed from an int.
A a1 = 5 ;

// Fine, class A can be implicitly constructed from an int.
f(3);
}
If you change class A's construct to be explicit, then A cannot be
implicitly constructed from an int, and both lines in main become errors.

--
Alan Johnson
Jun 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dario | last post by:
Trying to compile the following code-fragment with g++ 2.96: class Entity { private: void * data; public: explicit Entity(int); explicit Entity(Entity &); virtual ~Entity(); void...
2
by: Dave Theese | last post by:
Hello again, In the program below, the following line is of interest: bar.insert(make_pair(42, 17)); insert() expects a pair<foo<1>, foo<2> >. I'm giving it a pair <int, int>. But this is...
1
by: Stub | last post by:
Docs says that "The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction." I put up code of three cases...
6
by: Christoph Bartoschek | last post by:
Hi, gcc 3.4 rejects the following program: class T { public: T() : a(3) {} explicit T(T const & other) : a(other.a) {} private: int a;
12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
9
by: Tanmoy Bhattacharya | last post by:
Hi, This is a question about whether I am right that a particular syntactic sugar is missing in C++. Let me explain with an example. Let us say I have a class for complex numbers, and I want...
2
by: Dave | last post by:
Hello NG, Can anybody fathom the purpose of an explicit copy constructor? On page 232 of the Josuttis STL reference, I see a reference to such. How could you ever need to supress the...
2
by: Fred Zwarts | last post by:
Consider the following code: // Start of code class MyException_t { public: // Default constructor.
1
by: =?gb2312?B?wfXquw==?= | last post by:
Hi, folks, I am trying to make my copy constructor explicit, but when the scenario below comes into being, weird things happen. Here is the code snippet: #include <iostream> using...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.