returning an abstract class from a function

2019-01-18 02:40发布

Is it possible to return an abstract class(class itself or a reference, doesn't matter) from a function?

6条回答
疯言疯语
2楼-- · 2019-01-18 02:56

You can return an abstract class pointer - assuming B is a concrete class derived from abstract class 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>(...);
}
查看更多
劫难
3楼-- · 2019-01-18 03:06

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:

#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 !

查看更多
在下西门庆
4楼-- · 2019-01-18 03:07

The Factory design 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 instance of 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.

查看更多
祖国的老花朵
5楼-- · 2019-01-18 03:09

Abstract classes cannot be instantiated and thus not returned.

查看更多
Explosion°爆炸
6楼-- · 2019-01-18 03:12

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.

查看更多
我想做一个坏孩纸
7楼-- · 2019-01-18 03:15

You can declare the 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 class because 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.

查看更多
登录 后发表回答