C++ 从函数返回抽象类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2861270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
returning an abstract class from a function
提问by cemregoksu
Is it possible to return an abstract class(class itself or a reference, doesn't matter) from a function?
是否可以从函数返回抽象类(类本身或引用,无所谓)?
回答by
You can return an abstract class pointer - assuming B
is a concrete class derived from abstract class A
:
您可以返回抽象类指针 - 假设B
是从抽象类派生的具体类A
:
A * f() {
return new B;
}
or a reference:
或参考:
A & f() {
static B b;
return b;
}
or a smart pointer:
或智能指针:
std::unique_ptr<A> f() {
return std::make_unique<B>(...);
}
回答by Uri
You can declarethe return type to be a reference or pointer to the abstract class, so that it can be assigned to references or pointers to the abstract class and used based on its interface.
您可以将返回类型声明为抽象类的引用或指针,以便将其分配给抽象类的引用或指针,并根据其接口使用。
However, you cannot return an actual instance of the actual abstract classbecause by definition you cannot instantiate it. You could, however, return instances of concrete subtypes which is good enough because by the principle of substitution, you should always be able to use a subtype instead of a supertype.
但是,您不能返回实际抽象类的实际实例,因为根据定义您无法实例化它。但是,您可以返回具体子类型的实例,这已经足够好了,因为根据替换原则,您应该始终能够使用子类型而不是超类型。
回答by Justin Ethier
No, but a function could have a return type of a pointer (or a reference) to an abstract class. It would then return instances of a class that is derived from the abstract class.
不,但是函数可以具有指向抽象类的指针(或引用)的返回类型。然后它将返回从抽象类派生的类的实例。
回答by ThiefMaster
Abstract classes cannot be instantiated and thus not returned.
抽象类无法实例化,因此不会返回。
回答by Thomas Matthews
The Factorydesign pattern is an example of returning a pointer to an abstract class object:
的工厂设计模式是一个指针返回到一个抽象类对象的示例:
class Base
{ ; }
class One : public Base
{ ; }
class Two : public Base
{ ; }
Base * Factory(unsigned int number)
{
Base * p_item(NULL);
switch (number)
{
case 1:
p_item = new One;
break;
case 2:
p_item = new Two;
break;
default:
p_item = NULL;
break;
}
return p_item;
}
An actual abstract base class object can never be returned since there can never be an instanceof an abstract base class. Pointers and references to an abstract base type can be returned as in the above example.
永远不会返回实际的抽象基类对象,因为永远不可能有抽象基类的实例。可以像上面的示例一样返回对抽象基类型的指针和引用。
Pointers and references to an abstract base class returned from a function or method actually refer to a descendant of the abstract base type.
从函数或方法返回的对抽象基类的指针和引用实际上是指抽象基类型的后代。
回答by morandg
I know I'm little late but I hope this will help someone...
我知道我有点晚了,但我希望这会帮助某人......
Being a newbie in C++ programming, I've also been stuck for a while on that problem. I wanted to create a factory method that returns an reference to an abstract object. My first solution using pointers worked well but I wanted to stay in a more "C++ manner". Here is the code snippet I wrote to demonstrate it:
作为 C++ 编程的新手,我也被这个问题困扰了一段时间。我想创建一个返回对抽象对象的引用的工厂方法。我使用指针的第一个解决方案运行良好,但我想保持更“C++ 方式”。这是我为演示而编写的代码片段:
#include <iostream>
using std::cout;
using std::endl;
class Abstract{
public:
virtual void foo() = 0;
};
class FirstFoo: public Abstract{
void foo()
{
cout << "Let's go to the foo bar!" << endl;
}
};
class SecondFoo: public Abstract{
void foo()
{
cout << "I prefer the foo beer !" << endl;
}
};
Abstract& factoryMethod(){
static int what = 0;
if(what++ % 2 == 0)
return *(new FirstFoo());
else
return *(new SecondFoo());
}
int main(int argc, char* argv[])
{
int howMany = 10;
int i = 0;
while(i++ < howMany)
{
Abstract& abs = factoryMethod();
abs.foo();
delete &abs;
}
}
Open to any critism !
接受任何批评!