小编spi*_*rap的帖子

如何在实现文件中实现虚方法?

C++ noob问题:

假设我想使用一个带有多个方法的抽象类作为接口:

// this is AbstractBase.hpp

class AbstractBase
{
public:
  virtual int foo() = 0;
  virtual int bar() = 0;
  // imagine several more pure virtual methods are here
};
Run Code Online (Sandbox Code Playgroud)

我想要一个实现所有虚拟方法的子类,我不想在头文件中实现它们,而是在它们的实现文件中实现它们.

我是否真的必须在这样的子类声明中再次声明所有这些方法?我想应该有办法避免这一步(来自ObjC,Java)

// this is Concrete.hpp

class Concrete : public AbstractBase
{
public:
  int foo();
  int bar();
  // need to copy paste all the method declarations here again...? why?
};
Run Code Online (Sandbox Code Playgroud)

我想要做的是实现文件中的方法,如下所示:

// this is Concrete.cpp

#include "Concrete.hpp"

int Concrete::foo()
{
    return 666;
}

// etc...
Run Code Online (Sandbox Code Playgroud)

但无法弄清楚如何在不重新声明每个子类中的接口的情况下.

c++

15
推荐指数
2
解决办法
6043
查看次数

是否有公共 api 可以在现代 iOS 中获得更漂亮的圆角?

从 iOS 7 开始,Apple 一直在图标等视觉元素中使用更好的圆角。在iOS 11中,这些圆角在系统UI元素中非常丰富,比如控制中心等很多地方。

cornerRadius例如CALayer等上的API更基本。

是否有一些 API 可以为您提供新的圆润度?

例如,请参阅此问题: Draw iOS 7-style squircle programmatically

也看看这个问题的答案如何不好;几年后,是否有 Apple API 为您提供这种类型的拐角曲线?

ios

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

C++相当于简单的Java多态

我正在努力克服C++中看似简单的任务.我正在尝试实现基本的OOP多态性.

考虑这个Java示例:

interface Bob
{
   void foo();
}

class ActualBob implements Bob
{
   void foo()
   {
      /* I like foo things */
   }
}

class Jane
{
    Bob bob;
}
Run Code Online (Sandbox Code Playgroud)

简可以有任何鲍勃,很容易:

Jane jane = new Jane();
jane.bob = new ActualBob();
jane.bob.foo(); // actualbob things
Run Code Online (Sandbox Code Playgroud)

现在,在C++中,这似乎有点涉及......我需要输入什么来获得上述行为?

换句话说,我想有一个抽象基类的成员变量,但是想用它们做实际的实现.

class Bob
{
public:
   virtual void foo() = 0;
}

class ActualBob : public Bob
{
public:
    void foo(){/* I like foo things */}
}

class Jane
{
public:
    Bob bob;
}
Run Code Online (Sandbox Code Playgroud)

在这里采取捷径,但我想用C++做:

jane.bob.foo(); …
Run Code Online (Sandbox Code Playgroud)

c++ java polymorphism

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

标签 统计

c++ ×2

ios ×1

java ×1

polymorphism ×1