相关疑难解决方法(0)

虚函数和性能 - C++

在我的类设计中,我广泛使用抽象类和虚函数.我感觉虚拟功能会影响性能.这是真的?但我认为这种性能差异并不明显,看起来我正在做过早的优化.对?

c++ optimization performance virtual-functions

117
推荐指数
8
解决办法
6万
查看次数

方法链接+继承不能很好地结合在一起?

这个问题已在C++上下文中提出,但我对Java很好奇.关于虚拟方法的担忧不适用(我认为),但如果您遇到这种情况:

abstract class Pet
{
    private String name;
    public Pet setName(String name) { this.name = name; return this; }        
}

class Cat extends Pet
{
    public Cat catchMice() { 
        System.out.println("I caught a mouse!"); 
        return this; 
    }
}

class Dog extends Pet
{
    public Dog catchFrisbee() { 
        System.out.println("I caught a frisbee!"); 
        return this; 
    }
}

class Bird extends Pet
{
    public Bird layEgg() {
        ...
        return this;
    }
}


{
    Cat c = new Cat();
    c.setName("Morris").catchMice(); // error! setName returns …
Run Code Online (Sandbox Code Playgroud)

java inheritance method-chaining

38
推荐指数
4
解决办法
6626
查看次数

奇怪的重复模板 - 变化

关于CRP如果我想实现它的轻微变化(使用模板模板参数),我得到一个编译错误:

template <template <typename T> class Derived>
class Base
{
public:
    void CallDerived()
    {
        Derived* pT = static_cast<Derived*> (this);
        pT->Action(); // instantiation invocation error here
    }
};

template<typename T>
class Derived: public Base<Derived>
{
public:
    void Action()
    {
    }
};
Run Code Online (Sandbox Code Playgroud)

我不确定会选择这个表单(不能为我编译)而不是使用它(这可行)

template <typename Derived>
class Base
{
public:
    void CallDerived()
    {
        Derived* pT = static_cast<Derived*> (this);
        pT->Action();
    }
};

template<typename T>
class Derived: public Base<Derived<T>>
{
public:
    void Action()
    {
    }
};
Run Code Online (Sandbox Code Playgroud)

c++ templates crtp

8
推荐指数
2
解决办法
857
查看次数

C++:纯虚拟类型

我在C++(C++ 11)中探索模板恶作剧,我想要的一件事是抽象类中的纯虚拟类型.这就像Scala的抽象类型.在C++中,我想做类似以下的事情:

struct Base {
  // Says any concrete subclass must define Type, but doesn't
  // require that it be anything in particular.
  virtual typedef MyType; 
};

struct Derived : Base {
  // Won't compile unless this typedef exists.
  typedef int MyType;
};
Run Code Online (Sandbox Code Playgroud)

知道怎么做吗?

c++ templates scala generic-programming

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