相关疑难解决方法(0)

什么是mixin,为什么它们有用?

在" 编程Python "中,Mark Lutz提到了"mixins".我来自C/C++/C#背景,我之前没有听过这个词.什么是mixin?

这个例子的行之间进行读取(我已经链接到了因为它很长),我假设这是一个使用多重继承来扩展类而不是"正确"子类的情况.这是正确的吗?

为什么我要这样做而不是将新功能放入子类?就此而言,为什么mixin/multiple继承方法比使用组合更好?

mixin与多重继承的区别是什么?这仅仅是语义问题吗?

python oop multiple-inheritance mixins

875
推荐指数
16
解决办法
27万
查看次数

C++ std :: tuple破坏顺序

有一条规则说明std :: tuple的成员被破坏了吗?

例如,如果Function1返回一个std::tuple<std::unique_ptr<ClassA>, std::unique_ptr<ClassB>>to Function2,那么我可以确定(当Function2剩下的范围时)ClassB第二个成员ClassA引用的实例在第一个成员引用的实例之前被销毁吗?

std::tuple< std::unique_ptr< ClassA >, std::unique_ptr< ClassB > > Function1()
{
    std::tuple< std::unique_ptr< ClassA >, std::unique_ptr< ClassB > > garbage;
    get<0>(garbage).reset( /* ... */ );
    get<1>(garbage).reset( /* ... */ );
    return garbage;
}

void Function2()
{
    auto to_be_destroyed = Function1();
    // ... do something else

    // to_be_destroyed leaves scope
    // Is the instance of ClassB destroyed before the instance of ClassA?
}
Run Code Online (Sandbox Code Playgroud)

c++ std c++11 stdtuple

46
推荐指数
3
解决办法
2752
查看次数

我应该删除移动构造函数和智能指针的移动分配吗?

我正在实现一个简单的智能指针,它基本上跟踪它处理的指针的引用数量.

我知道我可以实现移动语义,但我不认为复制智能指针非常便宜.特别是考虑到它引入了产生令人讨厌的错误的机会.

这是我的C++ 11代码(我省略了一些不必要的代码).欢迎提出一般性意见.

#ifndef SMART_PTR_H_
#define SMART_PTR_H_

#include <cstdint>

template<typename T>
class SmartPtr {
private:
    struct Ptr {
        T* p_;
        uint64_t count_;
        Ptr(T* p) : p_{p}, count_{1} {}
        ~Ptr() { delete p_; }
    };
public:
    SmartPtr(T* p) : ptr_{new Ptr{p}} {}
    ~SmartPtr();

    SmartPtr(const SmartPtr<T>& rhs);
    SmartPtr(SmartPtr<T>&& rhs) =delete;

    SmartPtr<T>& operator=(const SmartPtr<T>& rhs);
    SmartPtr<T>& operator=(SmartPtr<T>&& rhs) =delete;

    T& operator*() { return *ptr_->p_; }
    T* operator->() { return ptr_->p_; }

    uint64_t Count() const { return ptr_->count_; }

    const T* …
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers move-semantics c++11

20
推荐指数
1
解决办法
3006
查看次数

Once_flag 可以移动吗?

这是怎么回事,标准没有提到once_flag的可移动性?我希望应用与 std::mutex 相同的参数。至少对于 gcc(4.8 版)来说,移动似乎被禁用了。如果某个编译器允许移动,那么最终可能会得到不可移植的代码。

c++ c++11

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