Extending an existing class like namespace (C ++)?

I write in the second person only because it is easy for you.

You are working with a game engine and really want a specific engine class to have a new method that makes "bla". But you do not want to distribute your game code in the "engine" code.

So, you can extract a new class from it with your new method and put this code in the source directory of your game, but maybe there is another option?

So this is probably completely illegal in C ++, but you thought at first: “Maybe I can add a new method to an existing class through my own header, which includes the parent header and some special syntax. Maybe while working with a namespace, for example ... "

Assuming you can't declare class methods across multiple headers (and you're pretty damn sure you can't), what are the other options that maintain a clean gap between “middleware / engine / library” and “application”, you wonder a question?

+4
source share
8 answers

My only question for you is: "Should your added functionality be a member function or can it be free?" If what you want to do can be resolved using the existing class interface, then the only difference is the syntax, and you should use a free function (if you think it is ugly, then ... suck it and move on, C ++ was not intended for monkeypatching).

If you are trying to break into the internal whales of a class, this may be a sign that the original class does not have sufficient flexibility (it does not provide enough information for you to do what you want from the open interface). If so, perhaps the original class may be "completed" and you will return to creating a free function on top of it.

If absolutely none of this works, and you should only have a member function (for example, the source class is provided to the protected members that you want to access, and you do not have the right to change the original interface) ... only then resort to inheritance and implementing member functions.

For in-depth discussion (and deconstruction of std::string '), check out this "Guru of the Week" article in "Monolith" .

+7
source

It sounds like an “act on” relationship that would not fit in inheritance (use sparingly!).

One option is the layout utility class, which acts on a specific instance of the Engine, creating an instance with a pointer to it.

+1
source
  • Inheritance (as you indicated) or
  • Use a function instead of a method or
  • Change the kernel code itself, but isolate and manage the changes with a patch manager such as a blanket or Mercurial / MQ

I do not see what is wrong with inheritance in this context.

+1
source

If the new method is implemented using the existing open interface, then perhaps this is more an object oriented to it as a separate function, rather than a method. At least Scott Myers claims that it is.

Why? Because it gives the best encapsulation. The IIRC argument states that the class interface must define the things that an object does. Helper functions are things that can be done with the / object, and not with what the object itself needs to do. Therefore, they do not belong to the class. If they are in a class, they can unnecessarily access private members and, therefore, extend the hide of this element and, therefore, the number of lines of code that need to be touched if the private member changes in any way.

Of course, if you want to access protected members, you must inherit. If your desired method requires a state for each instance, but not access to protected members, then you can inherit or combine to taste - the first is usually shorter, but has certain drawbacks if the relationship is not really "there".

+1
source

Looks like you want Ruby mixins. Not sure if something is close in C ++. I think you need to do inheritance.

Edit: perhaps you can use the friend method and use it as mixin, but I think that you will start to break your encapsulation in a bad way.

0
source

You can do something COM-like, where the base class supports the QueryInterface () method, which allows you to query the interface that has this method. This is pretty trivial to implement in C ++; you don't need COM yourself.

You can also “pretend” to be a more dynamic language and have an array of callbacks as “methods” and use the method of calling them using templates or macros and pushing 'this' on the stack to the rest of the parameters. But that would be crazy :)

0
source

Or categories in Goal C.

There are conceptual approaches to expanding cool architectures (rather than individual classes) in C ++, but this is not an accidental act and requires planning ahead of time. Unfortunately.

0
source

Sounds like a classic inheritance problem. In addition, I would drop the code in the "Engine Improvements" directory and incorporate this concept into your architecture.

0
source

All Articles