-
Notifications
You must be signed in to change notification settings - Fork 0
/
explicit.cpp
27 lines (25 loc) · 924 Bytes
/
explicit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
struct Vector2
{
int x, y;
// constructors are implicit by default
Vector2(int x, int y) : x(x), y(y) {}
explicit Vector2(int xy) : x(xy), y(xy)
{
// the explicit specifier prevents implicit conversions
}
Vector2() : x(0), y(0) {}
// note: constructors in this struct are not necessary,
// but they are here to demonstrate the explicit specifier
};
int main()
{
Vector2 v1 = {1, 2}; // implicit constructor
// equivalent to Vector2 v1(1, 2);
/*Vector2 v2 = 3;*/ // implicit constructor
// equivalent to Vector2 v2 = Vector2(3);
// we can't do this because of the explicit specifier
Vector2 v3 = Vector2(3); // explicit constructor
Vector2 v4(3); // explicit constructor
Vector2 v5 = (Vector2)3; // explicit constructor
Vector2 v6; // default constructor
}