标签: explicit-constructor

什么时候没有调用c ++构造函数?

我有一种情况,似乎没有调用构造函数:

#include <iostream>

using namespace std;

int main ()
{
    class yoyo
    {
        public:
        int i;
        yoyo()
        {
            i = 0;
            cout << "defaultly initialized to 0" << endl;
        }
        yoyo (int j) : i(j)
        {
            cout << "initialized to " << j << endl;
        }
    };

    int i;

    yoyo a;
    cout << "Hello1, i: " << a.i << endl;

    yoyo b(5);
    cout << "Hello2, i: " << b.i << endl;

    yoyo c = b;                                   /* 1 */
    cout << …
Run Code Online (Sandbox Code Playgroud)

c++ constructor explicit-constructor

6
推荐指数
2
解决办法
482
查看次数

显式构造函数仍然允许隐式转换

我正在用 C++14 绘制一个小的通用类型包装器模板,它旨在使用 mixins 启用、禁用或扩展底层类型的接口。

这是这个包装器的代码(精简了一点):

namespace detail {
    /// Helper template that is forwarded to the mixins used 
    /// for the extension of wrapper<•> in order to enable 
    /// access to the actual type `Derived`
    template <typename Derived>
    struct Cast {
        using type = Derived;

        template <typename T>
        static constexpr Derived& self(T* self) { return *static_cast<Derived*>(self); }

        template <typename T>
        static constexpr Derived const& self(T const* self) { return *static_cast<Derived const*>(self); }
    };

    /// This helper template is used …
Run Code Online (Sandbox Code Playgroud)

c++ constructor explicit-constructor implicit-conversion c++14

5
推荐指数
0
解决办法
360
查看次数

“多个运算符 + 与这些操作数匹配”错误

我正在为学校作业创建一个货币课程。我定义了从 Money 到 double 的转换,我有一个采用 int 的 Money 构造函数,另一个构造函数采用 double,并且我已经重载了“+”运算符以将 Money 类型的两个对象加在一起。当我尝试执行类似myMoney + 10myMoney 是 Money 类型的对象且 10 显然是整数的操作时,会出现错误消息。这是其余的相关代码:

class Money {
private:
    int dollars;
    int cents;
public:
    Money(double r);
    Money(int d) : dollars(d), cents(0) {}
    operator double();
}

Money operator+(Money a, Money b) {
    double r = double(a) + double(b);
    return Money(r);
}

Money::operator double() {
    return dollars+double(cents)/100;
}

Money::Money(double r) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试并且使两个构造函数显式显示,该程序实际上可以工作Money(double(myMoney)+10),但我不确定我是否理解自动转换会发生什么。谁能解释这种行为?

c++ operator-overloading type-conversion explicit-constructor implicit-conversion

4
推荐指数
1
解决办法
2万
查看次数

继承和显式构造函数?

请考虑以下代码:

template<typename T> class Base
{
    Base();
    Base(const Base<T>& rhs);
    template<typename T0> explicit Base(const Base<T0>&  rhs);
    template<typename T0, class = typename std::enable_if<std::is_fundamental<T0>::value>::type> Base(const T0& rhs);
    explicit Base(const std::string& rhs);
};

template<typename T> class Derived : Base<T>
{
    Derived();
    Derived(const Derived<T>& rhs);
    template<class T0> Derived(const T0& rhs) : Base(rhs); 
    // Is there a way to "inherit" the explicit property ?
    // Derived(double) will call an implicit constructor of Base
    // Derived(std::string) will call an explicit constructor of Base
};
Run Code Online (Sandbox Code Playgroud)

有没有办法以这样的方式重新设计此代码,Derived …

c++ constructor explicit-constructor c++11

4
推荐指数
1
解决办法
1720
查看次数

从Base派生的所有类型可用的隐式构造函数除了当前类型?

以下代码总结了我的问题:

template<class Parameter>
class Base {};

template<class Parameter1, class Parameter2, class Parameter>
class Derived1 : public Base<Parameter>
{ };

template<class Parameter1, class Parameter2, class Parameter>
class Derived2 : public Base<Parameter>
{
public :
    // Copy constructor
    Derived2(const Derived2& x);

    // An EXPLICIT constructor that does a special conversion for a Derived2
    // with other template parameters
    template<class OtherParameter1, class OtherParameter2, class OtherParameter>
    explicit Derived2(
        const Derived2<OtherParameter1, OtherParameter2, OtherParameter>& x
    );

    // Now the problem : I want an IMPLICIT constructor …
Run Code Online (Sandbox Code Playgroud)

c++ explicit-constructor implicit-conversion enable-if c++11

4
推荐指数
1
解决办法
519
查看次数

C++ - 我们为什么要在这个构造函数中使用explicit?

请参考维基百科:策略模式(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();
        }
};
Run Code Online (Sandbox Code Playgroud)

为什么在Context的构造函数中使用explicit是一个好习惯?

谢谢

c++ explicit-constructor

3
推荐指数
2
解决办法
2510
查看次数

在C++中放置新的VS显式构造函数调用

最近我遇到了这两种在内存中特定位置创建对象的方法:
1.

void* mem = malloc(sizeof(T));
T* obj = new(mem) T();
Run Code Online (Sandbox Code Playgroud)


2.

T* obj = (T*)malloc(sizeof(T));
*obj = T();
Run Code Online (Sandbox Code Playgroud)

第二种方式有点短......还有其他差异吗?关心马特乌斯

c++ explicit-constructor placement-new

3
推荐指数
1
解决办法
2016
查看次数

为什么转换构造函数优于转换运算符?

我有这个类SmallInt应该表示范围内的正整数值0-255- 包括:

struct SmallInt{
    explicit SmallInt(int x = 0) : iVal_( !(x < 0 || x > 255) ? x :
    throw std::runtime_error(std::to_string(x) + ": value outbounds!")){}
    operator int&() { return iVal_; }
    int iVal_;
};

int main(){

    try{
        SmallInt smi(7);
        cout << smi << '\n';
        cout << smi + 5 << '\n'; // 7 + 5 = 12
        cout << smi + 5.88 << '\n'; // 7.0 + 5.88 = 12.88
        smi = 33; // …
Run Code Online (Sandbox Code Playgroud)

c++ explicit-constructor conversion-operator implicit-conversion

3
推荐指数
1
解决办法
194
查看次数

是否有明确的副本

我正在寻找这样的语法:

class Hugo
{
    Hugo();
    explicit Hugo( const Hugo& hugo );

    Hugo GetRandomHugo()
    {
        Hugo hugo;
        hugo.value = rand();
                                  // this would fail:
                                  //    return hugo;

        return Hugo(hugo);        // explicit copy!!
    }
};
Run Code Online (Sandbox Code Playgroud)

换句话说:我正在寻找一种显式的复制语法,以允许方法返回一个副本,即使我的复制构造函数是显式的.

我正在使用GCC 4.4.5.

非常感谢,

查理

c++ gcc explicit-constructor

2
推荐指数
1
解决办法
533
查看次数

隐式定义的类的析构函数

考虑其不具有类的情况destructor,并constructor通过开发者明确声明.我知道在这种情况下destructor,一个班级implicitly declared就是这样.然后是真实的,destructorimplicitly defined,只有当一个类的对象是即将被销毁?

构造函数的行为也与上面相同.是implicitly defined仅在创建类的对象时?

编辑

class A {
  public:

};
int main() {

}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,将隐式声明~A().我的问题是,只有当类的对象被实例化时,是否真的会隐式地进行析构函数的定义.

class A {
      public:

    };
    int main() {
      A a;
    }
Run Code Online (Sandbox Code Playgroud)

或者是否隐式定义,即使没有进行对象实例化?

c++ constructor destructor explicit-constructor

2
推荐指数
1
解决办法
4161
查看次数