0

I define an abstract base class called Polygon which contains a method with signature like

int operator==(Polygon other)

I get an error while compiling which says

cannot declare parameter ‘other’ to be of abstract type ‘Polygon’

This was expected as Polygon was an abstract class and can not have objects. However when I replaced 'other' with '&other' the code compiled and even gave expected results.

From other posts on stack overflow I understood that when I define a function with paremeters preceded by '&' an alias to the passed argument is created. Now I have a variable called 'other' of type 'Polygon', which is not just a pointer. How can this happen as Polygon is an ABC? Is such code valid from a standards point of view?

Akilesh
  • 1,252
  • 1
  • 9
  • 16
  • In this respect references behave like pointers. They are not the object itself, they just refer to it, and can reference subtypes just like pointers. – Galik Oct 12 '14 at 15:20
  • 1
    http://stackoverflow.com/a/596750/635608; please pick up any decent C++ book/reference/tutorial and read about references. They're an important part of the language. – Mat Oct 12 '14 at 15:25
  • I am agree with Galik. You can think of it this way: then you pass parameter by value, you need to construct the object, impossible with abstract class. Passing by reference don't need this. – Alexey Birukov Oct 12 '14 at 15:26

3 Answers3

2

From other posts on stack overflow I understood that when I define a function with paremeters preceded by '&' an alias to the passed argument is created.

The correct term is "reference" rather than "alias" (which means something different in C++).

Now I have a variable called 'other' of type 'Polygon', which is not just a pointer.

No, the type of other is Polygon&, that is, reference-to-Polygon. You're right that it's not a pointer, but in many respects it behaves like a pointer. The main difference between pointers and references is that references can never point to nothing (whereas pointers can), and references can't be changed to point at something different once created.

How can this happen as Polygon is an ABC?

Polygon is an abstract base class, so you can't create one by itself. But let's say you have a concrete class like Rectangle which inherits from Polygon. When you pass your rectangle instance to operator==(Polygon& other), you're taking a reference to the Polygon inside the Rectangle, which is just fine.

Is such code valid from a standards point of view.

Not only is this type of "reference polymorphism" valid, but it's absolutely essential to how C++ works.

Tristan Brindle
  • 16,281
  • 4
  • 39
  • 82
1

First of all your question is not clear as to what you're intending to ask. As for as abstract base class is concerned, you cannot use plain objects as function arguments.

You can definitely use a reference/pointer to abstract class through which you can call virtual function of derived classes.For e.g

class Abstract
{
public:
  virtual void foo() = 0;
};

class Implementation : public Abstract
{
public:
  void foo() { std::cout << "Foo!" << std::endl; }
};

void call_foo(Abstract& obj) { obj.foo(); }
ravi
  • 10,994
  • 1
  • 18
  • 36
0

References in c++ behave like hidden pointers. Same polymorphic behaviour you can get with a pointer, you can achieve with a reference.

A reference to an abstract class is just like a pointer to an abstract class: it needs to reference an object of some non-abstract subclass of the abstract class. You can use a reference like that to call virtual methods on the referenced class using the . syntax.

iampranabroy
  • 1,716
  • 1
  • 15
  • 11