1
4

2 回答 2

1

You can not create objects of type SomeInterface::reference, since it is a pure abstract class, and that is what the compiler told you.

You need to return a reference (or a pointer) to such class. Like this :

virtual reference& operator[](unsigned int pos) = 0;

but then :

  1. in derived classes, you shouldn't change the signature of the pure virtual methods. It should stay virtual SomeInterface::reference& operator[](unsigned int pos)
  2. you can not return reference to a temporary object

btw take care how you create objects of such classes. They do not have virtual destructors.

于 2013-10-15T21:07:17.783 回答
1

Basically you can't return a reference to something that can never exist. However, you could use a pointer to an abstract class. That pointer will ultimately only be able to point to an instance of a derived class.

It is not clear exactly what you are trying to do. But you might look into Creation Patterns to find something close to what you need

于 2013-10-15T21:07:32.663 回答