小编ked*_*ede的帖子

C++中接口的多重继承

我有一个对象接口和一个派生对象可能想要支持的开放式接口集合.

// An object
class IObject
{
    getAttribute() = 0
}

// A mutable object
class IMutable
{
    setAttribute() = 0
}

// A lockable object 
class ILockable
{
    lock() = 0
}

// A certifiable object 
class ICertifiable
{
    setCertification() = 0
    getCertification() = 0
}
Run Code Online (Sandbox Code Playgroud)

某些派生对象可能如下所示:

class Object1 : public IObject, public IMutable, public ILockable {}
class Object2 : public IObject, public ILockable, public ICertifiable {}
class Object3 : public IObject {}
Run Code Online (Sandbox Code Playgroud)

这是我的问题:有没有办法编写只接受这些接口的某些组合的函数?例如:

void doSomething(magic_interface_combiner<IObject, IMutable, ILockable> object); …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance interface multiple-inheritance boost-mpl

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

用于指针迭代器的解引用适配器

我有一个容器,负责管理一组属性.该类部分看起来像这样:

class AttributeSet
{
public:
    // ... interface is irrelevant for my question.

private:
    std::vector<boost::shared_ptr<Attribute> > m_attributes;
};
Run Code Online (Sandbox Code Playgroud)

属性是多态的,因此属性必须存储为指针,但它们永远不能为NULL.

我想在BOOST_FOREACH中使用这个类,如下所示:

BOOST_FOREACH(const Attribute &attribute, attributeSet)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

根据BOOST_FOREACH文档,

对STL容器的支持非常普遍; 任何看起来像STL容器的东西都很重要.如果它嵌套了迭代器和const_iterator类型以及begin()和end()成员函数,BOOST_FOREACH将自动知道如何迭代它.

所以我更新了我的课程,看起来像这样:

class AttributeSet
{
public:
    typedef std::vector<boost::shared_ptr<Attribute> > container;
    typedef container::iterator iterator;
    typedef container::const_iterator const_iterator;

    iterator begin();
    iterator end();

    const_iterator begin() const;
    const_iterator end() const;

private:
    container m_attributes;
};
Run Code Online (Sandbox Code Playgroud)

所以现在我可以这样做:

BOOST_FOREACH(const boost::shared_ptr<Attribute> &attribute, attributeSet)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

这很好但是我不喜欢它将属性公开为指针.从呼叫者方面来说,这是噪音并且会产生无意义的NULL检查.

我对如何纠正问题有一些想法.例如,像这样的东西会很好:

class AttributeSet
{
public:
    typedef std::vector<boost::shared_ptr<Attribute> > container;
    typedef iterator_dereference_adapter< container::iterator …
Run Code Online (Sandbox Code Playgroud)

c++ boost iterator stl adapter

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

从命令行创建Intellisense数据库?

问题

是否可以在命令行中为解决方案(C++)构建Intellisense数据库?

上下文

我在一个相当大的C++代码库上工作.代码需要一段时间才能编译,所以我设置了一个本地夜间自动构建,我可以在任何时候想要开始一个新任务.我想在这个每晚构建期间为代码库的解决方案创建Intellisense数据库.我们正在使用Visual Studio 2013.

c++ intellisense command-line nightly-build visual-studio-2013

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

转换运算符正在切割我的对象

我从以下代码中获得了意外行为:

struct Base
{
    Base() {}
    virtual ~Base() {}

    virtual void foo() const = 0;

protected:
    Base(const Base &) {}
};

struct Derived : public Base
{
    Derived() {}
    Derived(const Derived &other) : Base(other) {}

    virtual void foo() const {}
};

struct NewDerived
{
    operator const Derived() { return Derived(); }
};

void func(const Base &b)
{
    b.foo();
}

int main()
{
    func(NewDerived());
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用MSVC2008,我在main()中得到此编译错误:

error C2248: 'Base::Base' : cannot access protected member declared in class 'Base'
Run Code Online (Sandbox Code Playgroud)

为什么要尝试访问Base的复制构造函数? …

c++ conversion-operator object-slicing

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