Can virtual functions be replaced with auto parameters?
Asked Answered
C

1

5

This question follows up on https://mcmap.net/q/45145/-why-do-we-need-virtual-functions-in-c

One of the classic examples of virtual functions is

class Shape
{
public:
    virtual string draw() = 0;
};

class Circle : public Shape
{
public:
    string draw() { return "Round"; }
};

class Rectangle : public Shape
{
public:
    string draw() { return "Flat"; }
};

void print (Shape& obj)
{
    cout << obj.draw();
}

However, we can instead pass an auto parameter in C++ 14

class Circle
{
public:
    string draw() { return "Round"; }
};

class Rectangle
{
public:
    string draw() { return "Flat"; }
};

void print (auto& shape)
{
    cout << shape.draw();
}

When should we prefer virtual functions or auto parameters?

Is the latter more efficient due to early binding?

Cyrillic answered 30/5, 2018 at 0:56 Comment(3)
possible duplicate of #1166463Bastogne
@vu1p3n0x That question can't be a duplicate; it makes no mention of auto at all.Cyrillic
The (main) purpose of inheritance is subtyping. There is no other subtyping feature.Washburn
P
8

C++ has two different mechanisms in place to write a piece of code that behaves differently based on the types of the objects acted on:

  • virtual functions and inheritance, which work at runtime, and
  • template functions, which work at compile-time.

Your example with auto parameters (which apparently weren't actually adopted in C++14 except for lambda functions) works with templates. The code you've written is equivalent to

template <typename T>
void print(T& shape) {
    cout << shape.name();
}

This code assumes that the type of T can be determined at compile-time, since the compiler needs to know the type of T in order to fill in the template. Once the compiler knows this it can say "Ah, I know what that type is! I'll generate code to directly call the name function in that type, and I know exactly what function that will call."

On the other hand, virtual functions and inheritance work at runtime. For example, suppose you want to write a function that reads some data from the network, then hands back either a Circle or a Rectangle. You might have some code like this:

Shape* myShape = decodeNetworkData();

Here, all the compiler knows is that myShape points to some sort of Shape, but it can't tell whether that's a circle or a square. Therefore, if you were to call

cout << myShape->name();

then the compiler would say "I know you're calling some version of name, but I don't know which one. But that's okay! I'll generate some code that looks at the dynamic type of myShape (the type of the thing it actually points at) and uses that to look up which function to call."

Notice that the code the compiler will generate in each case is different and the behavior will be different. In the first case, the compiler knows exactly which function to call. In the second, the compiler doesn't know what function to call, and has to generate some extra code to make things work. But, on the other hand, if you didn't have a Shape type with a virtual name function, you could make the "decode the bytes of the network" code snippet work with your first function, since the compiler would have to know, in advance, what type it was going to see come in over the network.

There was a proposal to mark this question as a duplicate of this older question on templates and inheritance, even though it's not superficially the same question. Once you know that the auto keyword in this context means "this is really a template function," you can look over that other proposed question to get some additional examples of the difference between static polymorphism (with templates) and runtime polymorphism (with virtual functions).

Poaceous answered 30/5, 2018 at 1:6 Comment(3)
in concise terms, auto gets demystified at compile time, so the compiler knows what type is getting introduced to the function at each call right ?Dozer
I know the difference between early and late binding (i.e. static vs runtime polymorphism) for virtual functions, but I didn't know whether auto uses early or late binding, which is the whole point of this question. That's why it's not a duplicate.Cyrillic
A template without the template keyword? That's new to me, and that's ugly!Washburn

© 2022 - 2024 — McMap. All rights reserved.