explicit-constructor

Why can I assign a QObject* to a QObject?

点点圈 提交于 2019-12-07 12:41:31
问题 Consider the following code: #include <QObject> class A : public QObject { Q_OBJECT public: A(QObject* parent = 0) : QObject(parent) {} } int main() { A a = new A(); return 0; } Why can I assign an object of type A* to a variable of type A without the compiler (or runtime) complaining? 回答1: In this code, the constructor of A is used to convert an A* to an object of type A , instead of assigning it. In general the compiler is allowed to implicitly use a matching constructor as a conversion

c++ copy initialization & direct initialization, the weird case

梦想与她 提交于 2019-12-07 02:09:06
问题 Before continue reading this, please read Is there a difference in C++ between copy initialization and direct initialization? first, make sure you understand what it is talking about. I'll summarize the rule here first (read standard n3225 8.5/16, 13.3.1.3, 13.3.1.4, and 13.3.1.5), 1) For direct initialization, all constructors will be considered as the overloading set, the overloading resolution will select the best one according to the overloading resolution rules. 2) For copy

Why can I assign a QObject* to a QObject?

六眼飞鱼酱① 提交于 2019-12-06 04:05:26
Consider the following code: #include <QObject> class A : public QObject { Q_OBJECT public: A(QObject* parent = 0) : QObject(parent) {} } int main() { A a = new A(); return 0; } Why can I assign an object of type A* to a variable of type A without the compiler (or runtime) complaining? In this code, the constructor of A is used to convert an A* to an object of type A , instead of assigning it. In general the compiler is allowed to implicitly use a matching constructor as a conversion operator, so that the following is legal code: struct B { B(int i){} } int main() { B b = 5; return 0; } In the

c++ copy initialization & direct initialization, the weird case

感情迁移 提交于 2019-12-05 06:12:20
Before continue reading this, please read Is there a difference in C++ between copy initialization and direct initialization? first, make sure you understand what it is talking about. I'll summarize the rule here first (read standard n3225 8.5/16, 13.3.1.3, 13.3.1.4, and 13.3.1.5), 1) For direct initialization, all constructors will be considered as the overloading set, the overloading resolution will select the best one according to the overloading resolution rules. 2) For copy initialization and the source type is the same as destination type or derived from destination type, the rule is as

Why is the std::bitset constructor with an unsigned long long argument not marked as explicit?

爱⌒轻易说出口 提交于 2019-12-05 02:05:38
The Standard Library class template std::bitset<N> has a constructor (C++11 and onwards, unsigned long argument before C++11) constexpr bitset(unsigned long long) noexcept Contrary to many best-practice guidelines, this single-argument constructor is not marked as explicit . What is the rationale behind this? TemplateRex Explicit construction The main objection against an explicit constructor is that copy-initialization from unsigned integers no longer works constexpr auto N = 64; std::bitset<N> b(0xDEADC0DE); // OK, direct initialization std::bitset<N> b = 0xDEADC0DE; // ERROR, copy

Explicit constructor taking multiple arguments

半世苍凉 提交于 2019-12-03 02:59:54
问题 Does making a constructor having multiple arguments explicit have any (useful) effect? Example: class A { public: explicit A( int b, int c ); // does explicit have any (useful) effect? }; 回答1: Up until C++11, yeah, no reason to use explicit on a multi-arg constructor. That changes in C++11, because of initializer lists. Basically, copy-initialization (but not direct initialization) with an initializer list requires that the constructor not be marked explicit . Example: struct Foo { Foo(int,

Explicit constructor taking multiple arguments

╄→гoц情女王★ 提交于 2019-12-02 15:01:26
Does making a constructor having multiple arguments explicit have any (useful) effect? Example: class A { public: explicit A( int b, int c ); // does explicit have any (useful) effect? }; Up until C++11, yeah, no reason to use explicit on a multi-arg constructor. That changes in C++11, because of initializer lists. Basically, copy-initialization (but not direct initialization) with an initializer list requires that the constructor not be marked explicit . Example: struct Foo { Foo(int, int); }; struct Bar { explicit Bar(int, int); }; Foo f1(1, 1); // ok Foo f2 {1, 1}; // ok Foo f3 = {1, 1}; //

C++ — Why should we use explicit in this constructor?

孤者浪人 提交于 2019-12-01 20:29:46
Please refer to Wikipedia:Strategy Pattern (C++) class Context { private: StrategyInterface * strategy_; public: explicit Context(StrategyInterface *strategy):strategy_(strategy) { } void set_strategy(StrategyInterface *strategy) { strategy_ = strategy; } void execute() const { strategy_->execute(); } }; Why it is a good practice to use explicit for the constructor of Context? Thank you Well, explicit constructors are always safe, but can be inconvenient. explicit ensures a compilation error should you provide a StrategyInterface* where a Context is expected. In doing so, it prevents

This is not copy-initializing, or is it?

本秂侑毒 提交于 2019-11-30 14:04:19
In the following code I am not allowed to declare an explicit ctor because the compiler says I am using it in a copy-initializing context (clang 3.3 and gcc 4.8). I try to prove the compilers wrong by making the ctor non explicit and then declaring the copy constructors as deleted. Are the compilers wrong or is there any other explanation? #include <iostream> template <typename T> struct xyz { constexpr xyz (xyz const &) = delete; constexpr xyz (xyz &&) = delete; xyz & operator = (xyz const &) = delete; xyz & operator = (xyz &&) = delete; T i; /*explicit*/ constexpr xyz (T i): i(i) { } };

What could go wrong if copy-list-initialization allowed explicit constructors?

余生颓废 提交于 2019-11-30 05:50:42
问题 In the C++ standard, §13.3.1.7 [over.match.list], the following is stated: In copy-list-initialization, if an explicit constructor is chosen, the initialization is ill-formed. This is the reason why we can't do, for example, something like this: struct foo { // explicit because it can be called with one argument explicit foo(std::string s, int x = 0); private: // ... }; void f(foo x); f({ "answer", 42 }); (Note that what happens here is not a conversion , and it would not be one even if the