开发者

returning an abstract class from a function

Is it possible to return an abstract class(开发者_如何学Goclass itself or a reference, doesn't matter) from a function?


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>(...);
}


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.


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.


Abstract classes cannot be instantiated and thus not returned.


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.


You can't return abstract class itself in the funtion return, but abstract class point and abstract class reference are both OK.

The only thing you need to worry is if you return by point, will the caller own the object and release, if you return by reference, be careful to return funtion's local variable, it compiles fine but it will crash. The function local variable will be released after the function call, like the following code, it will crash at line "a.print();". Hopefully it make sense.

class A {
public:
  virtual void print() = 0;
};

class B : public A {
    
    public:
    void print()
    {
        cout << "print";
    }
    
    A& GetA() {
        
        B b;
        return b;
    }
};

int main()
{
    cout<<"Hello World";
    
    B b;
    A &a = b.GetA();
    a.print();
    return 0;
}


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 !

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜