假设我有以下代码:
class Iinterface
{
virtual void abstractFunction()=0;
};
class Derived : public Iinterface
{
void abstractFunction(); // Do I need this line?
};
Derived::abstractFunction()
{
// implementation here
}
Run Code Online (Sandbox Code Playgroud)
如果我没有添加有问题的行,我会收到编译错误,说明abstractFunction未声明Derived.我正在使用VS 2008.
我不确定为什么我需要这个特定的行(不要把它与类声明之外提供的函数定义混淆),只要我继承Iinterface它就应该很明显我abstractFunction声明了.这是Visual Studio的问题还是由c ++标准强制执行?
不是从构造函数调用虚函数和纯虚函数的重复:
前一个问题涉及C++ 03,而不是C++ 11中新的构造函数委派行为,并且该问题没有通过使用委托来确保在执行纯虚拟实现之前正确构造来解决未定义行为的问题.
在C++ 11中,在构造期间在类的构造函数中调用Pure Virtual函数有什么危险,但是在通过构造函数委托"完全构造"类/对象之后?
显然,在C++ 11规范的某个地方存在这样的约束,
可以为正在构造的对象调用成员函数(包括虚拟成员函数,10.3).类似地,正在构造的对象可以是typeid运算符的操作数. - [C++工作草案]的12.6.2#13(http://www.open-std.org/jtc1/sc22/wg21/docs/ papers/2011/n3242.pdf)找不到已发布规范的"合理使用"版本.
一旦任何构造函数完成执行,C++ 11就会考虑构造一个对象.由于将允许多个构造函数执行,这意味着每个委托构造函数将在其自己类型的完全构造的对象上执行.派生类构造函数将在其基类中的所有委托完成后执行.- 维基百科说这是C++ 11的事情.
实际C++ 11参考未知.
以下示例在Visual Studio 2012 C++编译器的Nov CTP中编译和运行RUNS:
#include <string>
/**************************************/
class Base
{
public:
int sum;
virtual int Do() = 0;
void Initialize()
{
Do();
}
Base()
{
}
};
/**************************************/
// Optionally declare class as "final" to avoid
// issues with further sub-derivations.
class Derived final : public Base
{
public:
virtual int Do() override final
{ …Run Code Online (Sandbox Code Playgroud) 让我们说我们有一个具体的class Apple.(可以实例化Apple对象.)现在,有人来自class PeachApple 并从Apple中获取摘要.它是抽象的,因为它引入了一个新的纯虚函数.Peach的用户现在被迫从中派生并定义这个新功能.这是一种常见的模式吗?这是正确的吗?
样品:
class Apple
{
public:
virtual void MakePie();
// more stuff here
};
class Peach : public Apple
{
public:
virtual void MakeDeliciousDesserts() = 0;
// more stuff here
};
Run Code Online (Sandbox Code Playgroud)
现在让我们说我们有一个具体的class Berry.有人class Tomato从贝瑞那里得到一个摘要.它是抽象的,因为它覆盖了Berry的虚拟函数之一,并使其成为纯虚拟函数.Tomato的用户必须重新实现之前在Berry中定义的功能.这是一种常见的模式吗?这是正确的吗?
样品:
class Berry
{
public:
virtual void EatYummyPie();
// more stuff here
};
class Tomato : public Berry
{
public:
virtual void EatYummyPie() = 0;
// more stuff here
};
Run Code Online (Sandbox Code Playgroud)
注意:名称是人为的,并不反映任何实际代码(希望如此).在撰写这个问题时,没有任何成果受到伤害. 我本以为很多人都会想知道这是否可行,但我找不到任何重复的问题......请纠正我.
我只是想知道PHP是否提供纯虚函数.我想要以下内容
class Parent {
// no implementation given
public function foo() {
// nothing
}
}
class Child extends Parent {
public function foo() {
// implementation of foo goes here
}
}
Run Code Online (Sandbox Code Playgroud)
非常感谢.
该pure virtual destructor基类应该有一个定义.否则编译器将在链接时生成从派生类析构函数调用基类析构函数,并将导致链接错误.
我试图在基类中定义纯虚拟析构函数,如下所示:
class base
{
public:
base()
{
cout << "constructor in base class\n";
}
virtual ~base()=0
{}
};
Run Code Online (Sandbox Code Playgroud)
这给出了编译错误:
错误:函数定义的纯指定符
然后我尝试在基类之外定义函数,如下所示:
class base
{
public:
base()
{
cout << "constructor in base class\n";
}
virtual ~base()=0;
};
base::~base()
{
}
Run Code Online (Sandbox Code Playgroud)
这将删除编译错误,它的行为与我的理解相同.
但我的问题是如何在基类之外定义纯虚拟析构函数来消除编译错误?
我用纯虚方法声明了一些类,并使一些类派生它.Eclipse CDT中是否有任何方法可以找到父类的纯虚函数并自动完成子类的声明,就像在java开发中一样?必须来回复制和粘贴这些功能变得令人厌烦
在模板duck-typing和纯虚基类继承之间进行选择的指导原则是什么?例子:
// templates
class duck {
void sing() { std::cout << "quack\n"; }
};
template<typename bird>
void somefunc(const bird& b) {
b.sing();
}
// pure virtual base class
class bird {
virtual void sing() = 0;
};
class duck : public bird {
void sing() { std::cout << "quack\n"; }
}
void somefunc(const bird& b) {
b.sing();
}
Run Code Online (Sandbox Code Playgroud) 在基类中定义公共虚函数的好处是我们不必在派生类中重新定义它们.
即使我们在基类本身中定义纯虚函数,我们仍然必须在派生类中定义它们.
#include <iostream>
using namespace std;
class speciesFamily
{
public:
virtual void numberOfLegs () = 0;
};
void speciesFamily :: numberOfLegs ()
{
cout << "\nFour";
}
class catFamily : public speciesFamily
{
public:
void numberOfLegs ()
{
speciesFamily :: numberOfLegs ();
}
};
Run Code Online (Sandbox Code Playgroud)
这可能看起来很奇怪,但是在基类本身定义纯虚函数是否有益的情况下是否存在?
当有问题的类被分成*.h和时,我在实现从一些抽象类继承的纯虚函数时遇到了一些麻烦*.cpp.compiler(g++)告诉我,由于纯函数的存在,派生类无法实例化.
/** interface.h**/
namespace ns
{
class Interface {
public:
virtual void method()=0;
}
}
/** interface.cpp**/
namespace ns
{
//Interface::method()() //not implemented here
}
/** derived.h **/
namespace ns
{
class Derived : public Interface {
//note - see below
}
}
/** derived.cpp **/
namespace ns
{
void Derived::Interface::method() { /*doSomething*/ }
}
/** main.cpp **/
using namespace ns;
int main()
{
Interface* instance = new Derived; //compiler error
}
Run Code Online (Sandbox Code Playgroud)
这是否意味着我必须两次声明 …
我有一个C++学校的项目,我被困在一个部分:我必须重载运算符+和*来处理几何图形.这没有问题,但在这里它不起作用:我必须将操作符声明为纯虚方法,在所有其他类派生自的抽象类中.
#include<iostream>
using namespace std;
class Figabs {
protected:
int fel;
public:
int getFEL() { return fel; }
virtual Figabs operator +()=0; /*this is where I get an error: function returning abstract class “Figabs” is not allowed : function Figabs::operator+ is a pure virtual function */
};
class Coord {
public:
int cx, cy;
public:
Coord (){
cx = cy = 0;
}
Coord (const int x, const int y) {
cx = x;
cy = y;
}
Coord (const …Run Code Online (Sandbox Code Playgroud) pure-virtual ×10
c++ ×9
inheritance ×4
abstract ×2
base-class ×1
c++11 ×1
constructor ×1
delegation ×1
destructor ×1
eclipse-cdt ×1
overriding ×1
php ×1
polymorphism ×1
templates ×1