Hot answers tagged

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 ...
Alexander's user avatar
  • 3,712
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 ...
Euphoric's user avatar
  • 36.9k
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 ...
pjc50's user avatar
  • 10.8k
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 ...
Doc Brown's user avatar
  • 201k
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 ...
Erik Eidt's user avatar
  • 33.4k
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++ ...
pjc50's user avatar
  • 10.8k
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: ...
Flater's user avatar
  • 45.6k
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 ...
sfiss's user avatar
  • 725
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 ...
Doc Brown's user avatar
  • 201k
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 ...
gnasher729's user avatar
  • 42.7k
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 ...
Ewan's user avatar
  • 71k
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 ...
Hans-Martin Mosner's user avatar
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 ...
gnasher729's user avatar
  • 42.7k
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 ...
Alexander's user avatar
  • 3,712
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 ...
Jerry Coffin's user avatar
  • 44.4k
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: // ...
MrJ's user avatar
  • 111

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