# Key object implementation as a Union with operators

**URL:** https://community.keyboard.io/t/key-object-implementation-as-a-union-with-operators/798
**Category:** Kaleidoscope Internals
**Created:** [November 26, 2017, 1:04am UTC](https://community.keyboard.io/t/key-object-implementation-as-a-union-with-operators/798 "2017-11-26T01:04:54Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![dr-offig](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/dr-offig/32/759_2.png) [@dr-offig](https://community.keyboard.io/u/dr-offig)
#### Post date: [November 26, 2017, 1:04am UTC](https://community.keyboard.io/t/key-object-implementation-as-a-union-with-operators/798/1 "2017-11-26T01:04:54Z")

</div>

Probably just showing my ignorance here (self-taught programmer), but I was surprised to find the definition of the Key object in Kaleidoscope was via a union of the ‘object-data’ and operator definitions. I didn’t realise you could dereference a _this_ pointer in a union, and thought of a union as storing only one of its members at a time. I guess it works because it doesn’t need to store the operator definitions, since the symbol definitions get compiled into some global vtable. Pretty nifty, but my question is, why not use a normal C++ class? Is it because the language is not actually C++ (in which case, what is it?). Or is this something one does in embedded programming because it avoids some of the overhead of the full C++ class system?

---

<div class="post-metadata">

### Author: ![algernon](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/algernon/32/4404_2.png) [@algernon](https://community.keyboard.io/u/algernon)
#### Post date: [November 26, 2017, 7:33am UTC](https://community.keyboard.io/t/key-object-implementation-as-a-union-with-operators/798/2 "2017-11-26T07:33:41Z")

</div>

It was originally a union without operators. When we added the operators, we kept it as a union, because why not? It’s still simple, simpler than a class would be (in my opinion, at least), and gets the job done.

---

<div class="post-metadata">

### Author: ![noseglasses](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/noseglasses/32/429_2.png) [@noseglasses](https://community.keyboard.io/u/noseglasses)
#### Post date: [November 26, 2017, 7:54am UTC](https://community.keyboard.io/t/key-object-implementation-as-a-union-with-operators/798/3 "2017-11-26T07:54:43Z")

</div>

> [@dr-offig](#):
>
> Is it because the language is not actually C++ (in which case, what is it?).

It is C++ with some additional typedefs and libraries and without a `main(...)` function.

---

<div class="post-metadata">

### Author: ![dr-offig](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/dr-offig/32/759_2.png) [@dr-offig](https://community.keyboard.io/u/dr-offig)
#### Post date: [November 26, 2017, 8:53am UTC](https://community.keyboard.io/t/key-object-implementation-as-a-union-with-operators/798/4 "2017-11-26T08:53:10Z")

</div>

Nice! I like it! Learn something new every day.

---

<div class="post-metadata">

### Author: ![noseglasses](https://yyz1.discourse-cdn.com/flex031/user_avatar/community.keyboard.io/noseglasses/32/429_2.png) [@noseglasses](https://community.keyboard.io/u/noseglasses)
#### Post date: [November 26, 2017, 9:28am UTC](https://community.keyboard.io/t/key-object-implementation-as-a-union-with-operators/798/5 "2017-11-26T09:28:04Z")

</div>

Unions are a bit strange as they are kind-of classes.

From the C++ standard:

```auto
9.5 Unions
A union can have member functions (including 
constructors and destructors), but not virtual (10.3) 
functions. A union shall not have base classes. A 
union shall not be used as a base class.

```
