1

In 11.6.1 it says:

An aggregate is [snip] a class with

  • no virtual functions
  • [snip]

Why is that? Why didn't we want an aggregate to have virtual functions? What's the design rationale?

Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
  • Seems like a historical decision. Refer to the comments of [this answer](https://stackoverflow.com/a/23249967/4123703). – Louis Go Aug 04 '20 at 03:55

1 Answers1

0

Aggregate classes are a concept that strongly parallels C structs. They can be initialized using the same intializer list syntax. Were you to have virtual functions, it would require additional initialization to initialize the internal variables needed to support virtual functions.

The line is a little less crisp, now that we can have classes with intializer lists, but until C++11 the ability to initialize them with an initializer list was a major difference.

This difference is particularly useful for some C patterns. Consider Pthread's PTHREAD_MUTEX_INITIALIZER. It is a macro which initializes a Pthreads mutex in such a way that the first attempt to use it properly finishes the mutex initialization. This pattern is trivial in C, and there's a desire to have it work in C++ as well.

Speaking very informally, Aggregates and POD classes (and now standard layout) are the "what you see is what you get" classes. They closely parallel how things worked in C, permitting a great deal of interoperability.

Cort Ammon
  • 10,221
  • 31
  • 45