all 36 comments

sorted by: hot top controversial new old
[–] 68 points 2 years ago (2 children)

Why does your header file contain code?

  • source
  • hideshow 4 child comments
  • [–] 42 points 2 years ago (2 children)

    The stone-age called, they want their languages that need header files back!

    (I use Rust btw.)

  • source
  • hideshow 4 child comments
  • [+] 9 points 2 years ago (1 child)
  • [–] 2 points 2 years ago (1 child)

    I know that header files have some pretty horrible issues with templates and cyclic dependencies and so on, but from an organisational perspective I really like them. If I have to implement some complicated algorithm I could easily have a thousand likes + of Code, but the header still quite nicely shows the general structure in one or two screens. Whenever I do classes in python I start wishing for headers at like 300 lines, simply because I loose track of all the functions I already made.

    Maybe I'm just not a good python programmer tho.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 3 points 2 years ago

    I'd say at 1000 lines it usually makes sense to extract some parts into other files. But sure, I guess most obscurities have positive aspects. On the other hand, nothing is stopping you from writing a separate file with only function signatures next to your python scripts. It's just not required, because why would it ;)

  • source
  • parent
  • [–] 41 points 2 years ago* (3 children)

    Why are you putting the implementation in the .h file? You're supposed to declare in the header and implement in the .cpp files. The meme is reversed

  • source
  • hideshow 6 child comments
  • [–] 44 points 2 years ago (5 children)

    If C++/C were real languages for real programming they'd enforce this in the compiler.

    No sane language designer would say "it is imperative that you do not implement your class in its header file" then write a compiler designer to say "oh you implemented your code in the header file? lol lmao that rocks"

    They have played you all for fools.

  • source
  • parent
  • hideshow 10 child comments
  • [–] 18 points 2 years ago (1 child)

    I actually like how much freedom C++ gives you. As long as it is fed valid code, it does not give a fuck about how you choose to structure your project

  • source
  • parent
  • hideshow 2 child comments
  • [–] -4 points 2 years ago

    If C++/C were real languages for real programming they'd enforce unreadability in the compiler.

    No sane language designer would say "It is imperative that you write the most unreadable code possible" then write a compiler that says "oh your code doesn't triple dereference pointers? lol lmao that rocks"

    They have played you all for fools.

  • source
  • parent
  • [–] 12 points 2 years ago*

    There is an issue with templated code where the implementation does have to be in the header as well, though that is not the case here. C++20 introduced modules which I guess were meant to sort out this mess, but it has been a rocky road getting them to be supported by compilers.

  • source
  • parent
  • [–] -3 points 2 years ago (3 children)

    The reason is that header files are pretty much copy/pasted into your c files when you include them. so the code in them keeps getting recompiled for every c file, which drastically increases overall build times. If you only declare in the header and have one c file implementing the functions you compile them only once.

  • source
  • parent
  • hideshow 6 child comments
  • [–] 11 points 2 years ago (1 child)

    AI reply? That's the point they're trying to make. The implementation shouldn't be in the header file, but it is. Look at the image.

  • source
  • parent
  • hideshow 2 child comments
  • [–] -2 points 2 years ago (1 child)

    pretty sure the meme is about how the implementation looks ugly but using the implementation looks good because all the code is abstracted away. if it was like you said then why would they compare the code to the main.cpp

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 2 years ago

    I got confused. I was referring to the top-level comment in this thread, but that was actually posted by you, so... I got turned around.

    But yeah, sounds about right, that the meme could be referring to the horrors in the h file vs the clean cpp file. 😄

  • source
  • parent
  • [–] -3 points 2 years ago (1 child)

    drastically increases overall build times

    oh wow. from 0ms all the way up to 0ms. That's almost 1000 times faster wow.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 35 points 2 years ago (1 child)

    Looks like we've got a Java programmer here taking C++ for a spin.

  • source
  • hideshow 2 child comments
  • [–] 5 points 2 years ago (1 child)

    Yep, first thing that comes to mind is that header only classes need to use inline functions.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 4 points 2 years ago (1 child)

    I haven't codes anything in C++ for such a long time, I completely forgot how to.

  • source
  • hideshow 2 child comments