C++ Type Conversion
allow implicit conversion from a particular type on assignments.
Assignment operator allows what in terms of conversion?
y = (int) x;
C-like notation for type-casting
Promotion
Converting to int from some smaller integer type, or to double from float is known as...
y = int (x);
Functional notation for type-casting
Implicit conversion. This is also known as standard conversion.
The statement: short a=2000; int b; b=a; is an example of what?
allow implicit conversion to a particular type.
Type-cast operator allows what in terms of conversion?
allow implicit conversion from a particular type to initialize an object.
What does the member fxn Single-argument constructor allow in terms of conversion?
The type-cast operator uses a particular syntax: it uses the operator keyword followed by the destination type and an empty set of parentheses.
What is "operator" doing in the following sequence: // implicit conversion of classes: #include <iostream> using namespace std; class A {}; class B { public: // conversion from A (constructor): B (const A& x) {} // conversion from A (assignment): B& operator= (const A& x) {return *this;} // conversion to A (type-cast operator) operator A() {return A();} }; int main () { A foo; B bar = foo; // calls constructor bar = foo; // calls assignment foo = bar; // calls type-cast operator return 0; }
Loss of precision
What is a potential risk of implicit conversion?
constructors marked with explicit cannot be called with the assignment-like syntax
What is worth noting in the following sequence about 'explicit'?: // explicit: #include <iostream> using namespace std; class A {}; class B { public: explicit B (const A& x) {} B& operator= (const A& x) {return *this;} operator A() {return A();} }; void fn (B x) {} int main () { A foo; B bar (foo); bar = foo; foo = bar; // fn (foo); // not allowed for explicit ctor. fn (bar); return 0; } Edit & Run
Null pointers can be converted to pointers of any type Pointers to any type can be converted to void pointers. Pointer upcast: pointers to a derived class can be converted to a pointer of an accessible and unambiguous base class, without modifying its const or volatile qualification.
Which three conversions to pointers allow?