Hot answers tagged

6

The answer to your question is rather simple: Backwards-compatibility means not changing the meaning of existing code. Since there is no existing code using user-defined operators, because user-defined operators do not exist, introducing them cannot possibly break backwards-compatibility. Foo a = new Foo(); Foo b = new Foo(); Foo c = a + b; Such code ...


6

Because virtual functions have a runtime performance cost, and part of the philosophy of C++ is "you don't pay for what you don't use". Specifically, virtual methods have to dereference a pointer to find out where the correct method is for the specific object they were called on. One pointer dereference per call may not sound like a lot, but this also ...


5

I prefer NOT littering my code with null checks, because it's basically just clutter. Yes, sometimes it's necessary, like when reading user input or operating at application boundaries, getting stuff from third-party services etc. But keep your business logic clean. If a parameter can't be null, trust that it isn't null. In fact, you should try to avoid or ...


4

In case the map is modified in class A, objB should know the new map immediately. If by know immediately you mean assume the same state immediately without doing anything about it then B should just hold a reference to the map. You can protect it with unmodifiableMap. This approach is not multi threaded friendly since A is still free to mutate the ...


3

This is an easy one. If the preconditions are associated with the matchup (i.e. they always hold), then they belong in the constructor. If the preconditions are associated with the event (i.e. the event decides whether they hold or not), then they belong with the event.


3

You asked Instance variable G, why its always declared with capital? and Public interfaces with names like V, I am always taught to write descriptive names. In what conditions these names are allowed? I guess these two things have the same reason: the original algorithm description and the related sections in your text book has the form of a ...


3

If you're talking about variables of user-defined polymorphic types, then in C++ you need to use a pointer or a reference in order to achieve runtime polymorphism. The exact syntax you're describing is probably legal in C++ (hard to say since you didn't give a complete example), but results in "object slicing" rather than polymorphism. The reason is that a ...


2

I'd define a class containing all of the common stuff (is the list of booleans the same among these objects?) and their getters and setters, then subclass depending on the type of the changeable item within, then put them all into a container optimized for how you look up these things (id, probably). This way, you have one global.


2

Within the superclass, State, I have declared several variables which need to be accessed and changed from the 3 subclasses. That's what the protected modifier is for; "# Me and my Subclasses ...". Is it possible to instantiate these 3 subclass objects from the same "superclass object" so that they can all access the variables ... That you get ...


2

I normally perform these sorts of checks at the boundaries to sets of components e.g. for third party consumers. If you assert the incoming arguments for null-ness (and anything else) then your clients will get a clear message as to how they're misusing your API, and from that point on you can be reasonably safe in assuming that your input data is in a good ...


1

If you're going to serialize a class, it needs a serialVersionID. Your exception classes should always be serializable. You have no idea where the exception might be used, and if it gets marshaled across an app domain, you may lose debugging information or even lose the entire exception altogether. Exceptions are intended to be usable anywhere, and if ...


1

I think the most interesting difference between the two, certainly from the perspective of a language designer, is that Haskell's error handling is not a language feature but is part of the library, and yet it manages to be just as expressive and easy to work with as Java's. The reason that this works so nicely is due to Haskell's widespread use of Monads, ...


1

If you need to be able to assign a subclass instance to a variable in c++, and you need reassignment, then what you want is a pointer. private: State* current_state; If there's ownership involved, you should use a smart pointer like unique_ptr or shared_ptr. shared_ptr most closely matches the semantics of Java references, other than the fact that it ...


1

The problem is not that you are violating the Open/Closed principle, the problem is that you are violating the Single Responsibility Principle. This is literally a school book example of an SRP problem, or as wikipedia states: As an example, consider a module that compiles and prints a report. Imagine such a module can be changed for two reasons. First, ...



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