1

I am a beginner of c++ and I read google style guide. It suggests followings about interface class.

  1. It has only public pure virtual ("= 0") methods
  2. It may not have non-static data members.

I would like to know the reasons why it suggests

  • (a) interface class should not have default function and
  • (b) non-static data member? About question (b), is it related to initialization of class or serialization?

And this is more trivial and personal question,

  • (c) if I call "mix-in" class as "protocol" class personally, do you think of any problems? I try to name my mix-in classes with a marking name, like "Interface" in "ClassInterface", which is suggested by google style guide. "mix-in" class is not intuitive for me although I would like to show my respects to the name. "protocol" is intuitive for me because it is a kind of processes which are shared by players.

If someone knows the reasons or has any opinions. Please let me know them. Thank you very much.

bumble_bee_tuna
  • 3,533
  • 7
  • 43
  • 83
mora
  • 141
  • 8

1 Answers1

2

I'm not sure what you mean by not having default functions. Not having data members is basically to avoid various kinds of conflicts for a class that has multiple interfaces, or especially interfaces and a true base class.

For example, say that class X implements InterfaceA, which has member m_foo. If InterfaceB also has member m_foo, and X decides to inherit from InterfaceB as well and implement the interface, it's likely the existing implementation of X will be broken, as referring to m_foo in X will be ambiguous. See: c++ Multiple parents with same variable name

I should probably also add: not having member variables is a logical consequence of interfaces not providing any implementation. Since they don't do any work or run any code, there's no reason an interface class would need non-static member variables.

Edit: ok, so by defaulted functions, I think you mean, why does the style guide say the functions cannot have an implementation and must be pure virtual.

It again boils down to difficult cases in what's called multiple inheritance in c++. If the interface class methods had implementations, you could inherit them without re implementing. If you have two interfaces with the same method name and you inherit both, which function gets called? It's the same problem as with member variables.

Basically, this all comes back to the fact that multiple inheritance in c++ can be quite tricky. The rules for interfaces mimic an actual language construct in java, which does not have full multiple inheritance. With these restrictions, you can avoid these tricky situations, but you also lose some of the power of c++ full multiple inheritance.

See: What is the exact problem with multiple inheritance?

Community
  • 1
  • 1
Nir Friedman
  • 17,108
  • 2
  • 44
  • 72
  • Thank you Nir, it was very good answer for question (b). "not having default functions" means "default implementation". Google style guide suggests interface class has only public pure virtual ("= 0"). So it should not have any implementation. But I don't know why it is necessary. Thank you again. – mora Jul 08 '15 at 03:24
  • Thank you Nir, again. I could not think of collision of functions in multiple inheritances. Reference you gave me is also great. – mora Jul 08 '15 at 04:35