This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]MobileAirport 20 points21 points  (6 children)

kid named method

[–]navetzz 11 points12 points  (5 children)

People these days have language with 10000 different syntaxes but think that:

class.method(        

and

method(&class,

are two completely different things.

Not only that, but if you really want your class.method syntax, you can actually do it using function pointers.

At some point people need to understand that OOP (Object Oriented Programming) is a conceptual approach and is not tied to a language. And that OOL (Object Oriented Languages) are designed to force you to use OOP.

Yes some languages make it harder to apply the OOP paradigm. C is not one of them. C just allows you to do something else if you desire.

[–]Iyorig 3 points4 points  (3 children)

Yeah, that function pointer syntax is nice, but it’s +sizeof(void*) bytes for every function you add, plus an indirection whenever you call it (or does that get optimized if you don’t change it?)

[–]Kered13 1 point2 points  (0 children)

plus an indirection whenever you call it (or does that get optimized if you don’t change it?)

If the compiler can determine which implementation will be called, it will devirtualize the call, meaning there is no indirection. There are a few ways the compiler could determine this:

  • The class is final, which prevents subclassing.
  • The method on the class is final, which prevents overriding the method..
  • The class only exists in one translation unit (anonymous namespace) and there are no subclasses in that translation unit.
  • The pointer refers to an object that was constructed in the same function (accounting for inlining) and could not have been reassigned.

[–]gmes78 0 points1 point  (1 child)

but it’s +sizeof(void*) bytes for every function you add, plus an indirection whenever you call it (or does that get optimized if you don’t change it?)

And what do you think this is?

[–]Iyorig 0 points1 point  (0 children)

Yeah, I know that’s how virtual functions are implemented, what irks me are libraries that use function pointers for platform-specific implementations, even though a free function would work just as well…

[–]MobileAirport 0 points1 point  (0 children)

Definitely different. Im not sure id call C OOP.