void foo()
{
void* a[1];
a[1] = (void*)&bar;
}
explicit
, если хотите изменить это поведение.class Foo
{
public:
Foo(int v) : _v(v) {}
private:
int _v;
};
class Bar
{
public:
explicit Bar(int v) : _v(v) {}
private:
int _v;
};
void baz(Foo foo) {/* some code */}
void qux(Bar bar) {/* some code */}
int main()
{
Foo foo = Foo(4);
Bar bar = Bar(4);
baz(foo); // Ok.
baz(4); // Ok.
qux(bar); // Ok.
qux(4); // Fail.
}
If T is a class type, and the type of other is different, or if T is non-class type, but the type of other is a class type, user-defined conversion sequences that can convert from the type of other to T (or to a type derived from T if T is a class type and a conversion function is available) are examined and the best one is selected through overload resolution. The result of the conversion, which is a prvalue temporary if a converting constructor was used, is then used to direct-initialize the object. The last step is usually optimized out and the result of the conversion is constructed directly in the memory allocated for the target object, but the appropriate constructor (move or copy) is required to be accessible even though it's not used.Грубо говоря сначала то, что справа неявно приводится к типу слева (при помощи конструктора) а потом используется для инициализации переменно с помощью move или copy-конструктора.
#include <algorithm>
#include <iostream>
#include <string>
int main()
{
std::string strings[] = {"143", "224", "168", "162", "165", "226"};
char line[16] {};
std::transform(std::begin(strings), std::end(strings), std::begin(line),
[](const std::string& s) {
return std::stoi(s); });
std::cout << line;
}
protected:
double number;
public:
Matrix(double n):number(n){
///....
}
...
Matrix m = 10.2; //тут уже сработает конструктор и никаких приведений типов не нужно