40

Am I missing something about how the standard library is designed which justifies why these functions are free? The C++ standard library does not exclusively follow the OO design paradigm. Free functions, when combined with parameter overloading, play much nicer when you are writing templated code that should work with both class types and primitive types. ...


10

The explanations you quoted make sense from a semantical English point of view, but they are horribly ambiguous because it uses words that have very specific meaning in software development terms, but he's relying on the common English definition of these words. This book was released 31 years ago and I suspect that it was ahead of what is now considered ...


9

There is a school of though that prefers "non-member non-friend" functions, and justifies it in terms of improving code cohesion. I believe the original source for the idea is Effective C++ by Scott Meyers. The example in Bart's answer is a concrete demonstration of how this plays out in practice.


7

std::complex represents complex numbers, which is a mathematical concept. In mathematics it is customary to write f(x) instead of x.f(). So for more numerical or mathematical code, it is more natural to write norm(z) instead of z.norm(). (Also, there was and is lots of numerical code in C and Fortran, where the syntax goes norm(z).) (This is in addition to ...


2

Casting is a bad practice. Not just because it is potentially unsafe, but because it indicates a failed abstraction or some design issue. In this case, you really shouldn't want to "check for capabilities" on parking lots. What you should do instead is to ask the parking lot to perform its duties however it can. You can ask it to park() a car for ...


2

You've not abstracted this correctly. Inject a dependency (IDependency) which has a method which returns a boolean. Your service does not need to know how the dependency works, it just knows that it will receive a boolean. Based on your configuration, inject a different concrete class as the dependency. If you need to call a http client, inject ...


1

Considering that your model is the game logic (e.g. the territories, the tower, the ennemies, the rules of the game), the game loop would works this way: get next input event to process (mainly user input, i.e. a controller responsibility) translate the event into commands for the game logic (model) (the view will update itself if necessary since it's an ...


1

The basic argument here is that cohesion and reusability is improved if you don't create member functions that don't require access to private data. i.e. can be implemented efficiently using only member functions. Personally, I think C++ took that philosophy a bit too far, but I also think most libraries take it a bit too far the other way. For example, ...


1

For most users it would be nicer if C++ did the OOP thing(including giving methods to int, float..., but without making everything inherit from some MagicRootObject). Unfortunately there is a strong dislike for member functions(Herb Sutter even had a GOTW where he bashes 100+ string member functions, as if millions of developers that use string have to ...


1

It’s frequently taught that downcasting is bad practice. An advantage of interfaces is that a ParkingLotManager can store a list of ParkingLot objects, which can be FreeParkingLot, PaidParkingLot, etc. People doing #2 is precisely the reason why #1 is taught. What you're doing here is abusing polymorphism so you can cut a corner and store lots of different ...


1

I saw your now closed question on Code Review. Look at the Adapter design pattern. Think about that general idea. Existing classes are encapsulated by another class that exposes a desired uniform/standard interface. Thus Adapter.Bar would call ProviderFoo.Barrrrrr (whatever). Have an adapter for each "translation API" need. I see there will be many ...


1

In general, in a polymorphic design: Prefer to tell and not ask: instead of looking at each object and doing something depending on its class or abilities, tell the object what you want and let the object handle the operation (or a part of it, see template method pattern). But this is not desirable if it would require to add unrelated responsibilities to a ...


1

Starting out with the original question Well, casts are there for a reason, so they cannot be inherently bad. BUT: if you use them in the way you describe, i.e. your ParkingLotManager can only return the list of all parking lots, and then you have to filter on the client side, this is smelly. Normally, you'd have your parking lots organized in some kind of ...


1

If "ability to handle money" is a property, then it's a boolean one, and it's used to know whether the machine is capable of handling money or not. If it's non-static and mutable then it can be used to prevent the machine from handling money by setting it to false. Machine m = new Machine(); if (m.canHandleMoney()) { // this machine can handle ...


Only top voted, non community-wiki answers of a minimum length are eligible