views:

140

answers:

6

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

+2  A: 

Abstract classes cannot be instantiated and thus not returned.

ThiefMaster
What about the reference?I know that it is not possible to pass an abstract class as a parameter to a function, but i could pass the reference of the abstract class.
cemregoksu
Only if the references actually refers to an instance of a child class.
Steven Sudit
cemregoksu
+1  A: 

You cannot return an actual class (a blueprint for creating objects). You can return an instance of a class though.

CrazyJugglerDrummer
+4  A: 

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.

Justin Ethier
You mean "return type of *a reference to* an abstract class".
avakar
I couldnt understand what you mean. can you give a little example?
cemregoksu
@incrediman: then he's wrong.
avakar
IBM is not a good source - that site has some truly crappy information on it.
anon
@incrediman, and I can return references to (an object of) a class.
avakar
@Neil: Are you sure you are commenting on the right answer?
sbi
@sbi: The OP didn't say that the function would be abstract. He said that the function's return type would be an abstract class. Like this: `Foo f(){...}` where Foo is an abstract class. Which is perfectly valid. Edit: sbi deleted his comment.
Cam
@incrediman: `abstract f()` is not valid in C++ if `abstract` is an abstract class.
sbi
@sbi: I'm not sure I understand. If I have an abstract base class called Shape and a concrete child named Circle, why can't I define foo() to return a Shape reference or pointer?
Steven Sudit
@sbi Yes - the answer has been edited repeatedly within the initial grace period so the edits don't show up.
anon
@Steven Sudit, you can, but it has to be a reference or a pointer. Read sbi's comment again.
avakar
@sbi: Turns out you're right :)Can you explain why that is? Edit: Actually it's because you cannot instantiate an instance of the class, so it has to be a pointer.
Cam
@avakar: Oh, ok. Yes, you can't return a copy of the ABC, just a reference (or pointer) to it. That's because no ABC can be instantiated, by definition.
Steven Sudit
then thank you all=)
cemregoksu
+12  A: 

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.

Uri
+7  A: 

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;
}
anon
i want to use the abstract class itself (or its reference), to use it as a generic form for other classes which are derived from that abstract class
cemregoksu
@cemregoksu That's the only purpose for having an abstract class. Both the functions above illustrate how you would do this with respect to function return values.
anon
thanks for your help. i think it worked=)
cemregoksu
A: 

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.

Thomas Matthews