小编maz*_*ork的帖子

移动构造函数和const成员变量

我喜欢const成员变量的想法,特别是当我将C函数包装到类中时.构造函数获取在整个对象生命周期内保持有效的资源句柄(例如文件描述符),析构函数最终将其关闭.(那是RAII背后的想法,对吧?)

但是使用C++ 0x移动构造函数我遇到了问题.由于析构函数也在"卸载"对象上调用,我需要防止资源句柄的清理.由于成员变量是const,我无法分配值-1或INVALID_HANDLE(或等效值)来向析构函数指示它不应该执行任何操作.

有没有办法在对象的状态被移动到另一个对象时不调用析构函数?

例:

class File
{
public:
    // Kind of "named constructor" or "static factory method"
    static File open(const char *fileName, const char *modes)
    {
        FILE *handle = fopen(fileName, modes);
        return File(handle);
    }

private:
    FILE * const handle;

public:
    File(FILE *handle) : handle(handle)
    {
    }

    ~File()
    {
        fclose(handle);
    }

    File(File &&other) : handle(other.handle)
    {
        // The compiler should not call the destructor of the "other"
        // object.
    }

    File(const File &other) = delete;
    File &operator =(const File &other) …
Run Code Online (Sandbox Code Playgroud)

raii move-constructor c++11

27
推荐指数
3
解决办法
6240
查看次数

如何从容器中删除指针中的unique_ptr?

使用unique_ptr创建对象并为容器提供所有权是没有问题的.如何通过原始指针删除元素?

std::set<std::unique_ptr<MyClass>> mySet;

MyClass *myClass = new MyClass();
mySet.insert(std::unique_ptr<MyClass>(myClass));

// remove myClass from mySet?
Run Code Online (Sandbox Code Playgroud)

unique-ptr c++11

7
推荐指数
1
解决办法
4044
查看次数

通过明确排序避免死锁

我想明确提供一个关于MySql InnoDB如何获取行锁的命令.如果可能的话,不应该有任何死锁只是停滞.(如果我们遵循惯例.)

首先,数据库应按升序锁定表"模型"中的所有行.然后第二个表"颜色"中的所有行应按升序锁定.有没有办法控制数据库首先锁定表"模型"然后"颜色"?

举个例子:

start transaction;
select *
from models m
join colors c on c.model_id = m.id
where c.id IN (101, 105, 106)
order by m.id asc, c.id asc
for update;
Run Code Online (Sandbox Code Playgroud)

mysql sql innodb locking pessimistic-locking

7
推荐指数
1
解决办法
524
查看次数

Android的requestLayout()是否适合布局动画?

requestLayout()方法是在Android中创建动画布局的正确工具吗?

我们用Adobe/Apache Flex工作了几年,并且知道这两种方法invalidateProperties()commitProperties().它似乎是Android的requestLayout()并且layout()服务于类似的目的.但是文档提到有一个开销.

public class Paginator extends ViewGroup {
    private float animatedCurrentPage = 1;
    private final ObjectAnimator objectAnimator;

    public Paginator(Context context, AttributeSet attrs) {
        super(context, attrs);
        objectAnimator = ObjectAnimator.ofFloat(this, "animatedCurrentPage", 0f);
    }

    public void jumpToPage(int page) {
        objectAnimator.cancel();
        objectAnimator.setFloatValues(animatedCurrentPage, page);
        objectAnimator.start();
    }

    public void setAnimatedCurrentPage(float animatedCurrentPage) {
        this.animatedCurrentPage = animatedCurrentPage;
        requestLayout(); // <== queue an update of the layout
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { …
Run Code Online (Sandbox Code Playgroud)

android android-layout

7
推荐指数
1
解决办法
1431
查看次数

Criteria Api:使用CriteriaBuilder创建空值

有没有更好的方式来创建一个SQL null与价值CriteriaBuilder

criteriaBuilder.quot(criteriaBuilder.literal(0), criteriaBuilder.literal(0))
Run Code Online (Sandbox Code Playgroud)

java jpa criteria-api jpa-2.0

5
推荐指数
1
解决办法
2981
查看次数