15
votes
C# and C++ Inheritance and Performance - Shouldn't Compilers Handle this Issue?
Don't we need to take a step back here?
Under the hood, it generally all boils down to simply functions being called the with the this pointer as first arg.
It's good to question things from first ...
9
votes
C# and C++ Inheritance and Performance - Shouldn't Compilers Handle this Issue?
Imagine this extremely simple example
A value = Random.Shared.Next(0, 2) == 0 ? new B() : new C();
value.M();
abstract class A
{
public abstract void M();
}
class B : A
{
public override ...
6
votes
Accepted
Should I use private field or static variable in my method?
They mean different things: static variable within the method is the same across all objects, whereas private field can have different values for different objects.
You can also have a private static ...
4
votes
Accepted
C++ creating a class with self referencing custom comparator
Old wisdom from C programming: when you are implementing two functions referencing each other recursively, it is all a matter of proper forward declarations.
This is not much different in C++: you ...
4
votes
C# and C++ Inheritance and Performance - Shouldn't Compilers Handle this Issue?
Why can't the compiler/jitter optimize out the call resolutions at compile time?
It can in some cases, but there are reasonable scenarios that preclude compile-time identification of the proper ...
3
votes
How to bind C/C++ functions and classes for my language
Approaches vary, because it depends how your language works, but the general technique:
load the native code as a library using e.g. "dlopen()"
identify the function name (see "C++ ...
3
votes
C# and C++ Inheritance and Performance - Shouldn't Compilers Handle this Issue?
Investigating your underlying claim
For the purpose of example, let's say that you have a codebase where there is a MyType class, and there is also some kind of base-type-oriented logic, let's say:
...
2
votes
Is there a distinct optional type semantically representing a value that *needs to be calculated later*?
Some concepts come to mind (you would need to do some own research for C++/Rust, I only linked a first result):
Lazy<T>: A value that will be computed at a possible later time, but is ...
2
votes
Is there a distinct optional type semantically representing a value that *needs to be calculated later*?
Yes, sort of. It is part of almost any major programming language: it is called a first-class function.
Instead of passing a specific value v, one passes a function f around which can calculate v on ...
2
votes
C# and C++ Inheritance and Performance - Shouldn't Compilers Handle this Issue?
The whole point of virtual function calls is that at compile time it is unknown which code will be executed.
Swift has a partial solution: You can specify whether a class can be subclassed in a ...
1
vote
C# and C++ Inheritance and Performance - Shouldn't Compilers Handle this Issue?
Why can't the compiler/jitter optimize out the call resolutions at
compile time?
It can, you help it by marking some classes as sealed, The linter also knows that if a class is internal, and has no ...
1
vote
Is there a distinct optional type semantically representing a value that *needs to be calculated later*?
There is a range of possibilities, covering the spectrum from lenient to strict.
You did not explicitly state whether the value can be computed lazily when a client needs it, or is computed in a later ...
1
vote
Is const appropriate in this situation?
const in C++ has two meanings: For objects, it means they cannot be modified and any attempt to modify them invokes undefined behaviour. For data referenced by a const pointer or const reference, it ...
1
vote
Is const appropriate in this situation?
const is an interesting thing in interfaces, because it doesn't mean "at least readable". It means "not writable," which is inherently restricting.
While I'm a fan of using const ...
1
vote
Is const appropriate in this situation?
I think Phillip Kendall's answer is reasonable, but somewhat incomplete.
At least in my view, what you would typically want for a case like this is to separate the physical implementation from what it ...
1
vote
Best way to access (grand grand) parent element in gui
I encountered this same problem and solved it via a static member function of Foo that traverses parents of a child widget until one of type Foo is found:
class Foo : public QWidget {
public:
// ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
c++ × 2716c × 285
design × 253
object-oriented × 232
design-patterns × 188
java × 153
c++11 × 132
coding-style × 98
c# × 96
object-oriented-design × 92
data-structures × 78
multithreading × 75
programming-practices × 71
algorithms × 70
performance × 70
programming-languages × 68
qt × 65
compiler × 60
unit-testing × 59
inheritance × 59
libraries × 57
python × 55
memory × 53
architecture × 52
templates × 52