• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 09:52
CEST 15:52
KST 22:52
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
TL.net Map Contest #22 - Voting & Ladder Map Selection1Code S Season 2 (2026) - RO8 Preview4[ASL21] Finals Preview: Two Legacies21Code S Season 2 (2026) - RO12 Preview2herO wins GSL Code S Season 1 (2026)7
Community News
StarCraft II 5.0.16 PTR Patch Notes may 26th110Weekly Cups (May 18-25): MaxPax wins doubles0Crank Gathers Season 4: BW vs SC2 Team League5Weekly Cups (May 11-17): Classic wins double1Code S Season 1 (2026) - RO8 Results2
StarCraft 2
General
StarCraft II 5.0.16 PTR Patch Notes may 26th The death of cheese, from a professional cheeser. TL Poll: How do you feel about the 5.0.16 PTR balance changes? Changing from 12 to 8 is just asking for StarCraft Weekly Cups (May 11-17): Classic wins double
Tourneys
RSL Revival: Season 5 - Qualifiers and Main Event Crank Gathers Season 4: BW vs SC2 Team League GSL Code S Season 2 (2026) Maestros of The Game 2 announcement and schedule ! Sparkling Tuna Cup - Weekly Open Tournament
Strategy
[G] Having the right mentality to improve
Custom Maps
[D]RTS in all its shapes and glory <3 [A] Nemrods 1/4 players
External Content
The PondCast: SC2 News & Results Mutation # 528 Infection Detected Welcome to the External Content forum Mutation # 527 Hell Train
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ Quality of life changes in BW that you will like ? Soma's ASL Finals Review FlaShFTW vs A.Alm Grudge Match Event VPN experiences
Tourneys
[ASL21] Grand Finals [Megathread] Daily Proleagues Escore Tournament StarCraft Season 2 [BSL22] WB Final & LB Semis - Saturday 21:00 CEST
Strategy
Any training maps people recommend? Muta micro map competition [G] Hydra ZvZ: An Introduction Fighting Spirit mining rates
Other Games
General Games
Warcraft III: The Frozen Throne Stormgate/Frost Giant Megathread Nintendo Switch Thread ZeroSpace Megathread Path of Exile
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Vanilla Mini Mafia Mafia Game Mode Feedback/Ideas TL Mafia Community Thread Five o'clock TL Mafia
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Trading/Investing Thread Things Aren’t Peaceful in Palestine Dating: How's your luck?
Fan Clubs
The herO Fan Club!
Media & Entertainment
[Manga] One Piece Anime Discussion Thread [Req][Books] Good Fantasy/SciFi books
Sports
McBoner: A hockey love story 2024 - 2026 Football Thread TeamLiquid Health and Fitness Initiative For 2023 Formula 1 Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread Facing Challenges in Mobile App Development
TL Community
The Automated Ban List
Blogs
Esportsmanship: How to NOT B…
TrAiDoS
Why RTS gamers make better f…
gosubay
ramps on octagon
StaticNine
ASL S21 English Commentary…
namkraft
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1925 users

Write everything in .h?

Blogs > SlimeBagly
Post a Reply
SlimeBagly
Profile Blog Joined August 2010
356 Posts
Last Edited: 2011-12-04 18:18:57
December 04 2011 18:13 GMT
#1
Hello TL,
As a long-time Java programmer, both professionally and as a past time, I figured I ought to finally break down and learn C/C++. Boy, it seems inefficient! It seems like you have to write so much code to do object oriented stuff than in java. Really? Every time I make a new class I need a .cpp file AND a .h file? Really? every time I add a new method, I have to also add its def to the .h? Bizarre.

Then I noticed, all these tutorials mention as a side note, that you can directly implement stuff in the .h file. Well, fuck. Why not just implement the whole class in there? If I want to make something like a (java) abstract class or a (java) interface, I'll do that separately, but if I can build a concrete class in a single file, why not?

What am I missing here?

Also, what editor do you guys use for C++? As an old java salt, I immediately set up Eclipse with c++, but it is... lackluster. The quick fix suggestions are non existent, and I'd like to think that any good editor should just build the .h file for you.


Also, after writing this, I realized, that it may fall in the category of "TL is not yahoo answers." if so, apologies are in order.

( I very nearly gave up on c++ syntax when I shifted cout "hello world" bits to the left... that looks so wrong)

*
mutalisks are awesome!
tarpman
Profile Joined February 2009
Canada723 Posts
December 04 2011 18:24 GMT
#2
The idea is to separate the declarations from their implementation. Your header file (.h) will be included in every file that uses or references your classes. On the other hand, the implementation (.cpp file) should only be compiled once.

So, to answer your question:

- You need to put declarations in the .h file, so that other .cpp files besides the main one can reference those classes and functions.
- You can't just implement functions in the .h file, because if that file gets included in two different .cpp files (for instance) your build will fail when the linker notices that the same function is defined in two places.
Saving the world, one kilobyte at a time.
Dead9
Profile Blog Joined February 2008
United States4725 Posts
December 04 2011 18:41 GMT
#3
you might be better off posting here http://www.teamliquid.net/forum/viewmessage.php?topic_id=134491

i'm not an expert or anything, but here goes:

implementing everything in the .h file forces the compiler to compile it every time it is used via #include
so if your header file is used a lot you can save some compile time by splitting it up

as far as i know the best c++ editor is microsoft visual studio
http://www.microsoft.com/visualstudio/en-us
for free: https://www.dreamspark.com/default.aspx
there are a bunch of pure text editors that are pretty popular too
vim: http://www.vim.org/download.php
emacs: http://www.gnu.org/s/emacs/
notepad++: http://notepad-plus-plus.org/
personally i use notepad++ since it's easy to set up and get used to

for building the .h file automagically, you should take a look at makefiles
bbm
Profile Joined April 2011
United Kingdom1320 Posts
December 04 2011 19:32 GMT
#4
On December 05 2011 03:24 tarpman wrote:
- You can't just implement functions in the .h file, because if that file gets included in two different .cpp files (for instance) your build will fail when the linker notices that the same function is defined in two places.

Worth pointing out that you should avoid this anyway, because this will happen to anything, not just a function (I believe).

You avoid this by having this wrapping around the contents of your .h file:

#ifndef myfileH
#define myfileH

... header contents ...

#endif


This means that if you include the same header twice (over a variety of headers that all include each other in complex ways, for example), the first time it reads during precompilation (when it deals with includes, defines, etc) it it'll create a define called "myfileH", and then the next time it comes across the file, it will fail the "ifndef" (if not defined) rule and skip out the header entirely.



It's bad coding practice to have big headers. In big projects, you really want to cut down on how much goes in to your headers. Headers may be compiled several times (i think) over different files in your project, but a .cpp will only be compiled once, so the smaller they are, the better.

For example, you can use forward declarations to help. Say you have a header that looks like the following:

class A
{
void blah(B* input);
}

Rather than including b.h, you can write

class B;
class A
{
void blah(B* input);
}

and then in the .cpp have the include "b.h" there. It'll just keep a record that it's expecting a B, but wont worry about what B actually is until it needs to.

Also look up the ideas behind PIMPL, which basically involve putting all private methods inside a struct in the cpp, so the only things in the header files are public method declarations
By.Sun or By.Rain, he always delivers
spinesheath
Profile Blog Joined June 2009
Germany8679 Posts
December 04 2011 20:34 GMT
#5
If you are on Windows, use Visual Studio. Pretty much the best C++ IDE available right now. If you're a student, try to get it for free via MSDNA.
For Linux I haven't found anything better than Eclipse yet, but it's definitely not as good as VStudio.
If you have a good reason to disagree with the above, please tell me. Thank you.
KeksX
Profile Blog Joined November 2010
Germany3634 Posts
Last Edited: 2011-12-04 20:45:31
December 04 2011 20:34 GMT
#6
The best way to explain this is this:

Imagine being in a big project with classes that have tons of functions. Now imagine that, in the middle of development, there's no full documentation yet.

Now you want to know how a class works, names and declarations etc.
By seperating Definition from Declaration, you could just look into the header and immidiately know whats going on. Now imagine you have a header file that is 2000 lines long. Have fun reading that!

It is just for organizing code and have a better workflow. You don't have to do that, but it is highly recommended to do so.
AcrossFiveJulys
Profile Blog Joined September 2005
United States3612 Posts
December 04 2011 20:58 GMT
#7
c++ is a total joke for OOP. annoying header files, include loops, and the necessity to overload the copy constructor in many situations and other c++ BS (e.g. manual memory management) has made me switch entirely to java/scala. The java compiler/JVM has come a long way so that compiled c++ isn't really that much more efficient than bytecode anymore, so there's little reason to continue using this language.
mmp
Profile Blog Joined April 2009
United States2130 Posts
Last Edited: 2011-12-04 21:14:30
December 04 2011 21:01 GMT
#8
Header files are not intended to make your life better or worse. They are part of a convention around a necessary aspect of C. The file extensions do not mean anything unless your compiler/preprocessor expects extensions. A header file versus a source file is just a separation of form from functionality. You could put your declarations in a c file if it's convenient. You can write definitions inside of a header file, but this is discouraged because headers are expected to be idempotent with respect to the load order. So if you reload a header file and don't use #ifdef or similar preprocessing to check that it has already been loaded, then the compiler will yell at you for trying to redeclare something.

Declarations (function, struct, class, or otherwise) are not intended to be a "sneak peak" at what the definition will later implement. They are intended for code that does not have access to the definitions at compile time (but before linking) to properly read from the stack (C is one abstraction level above assembly, but there are deep concepts at work). A function declaration essentially tells the compiled code the size of the arguments on the function stack so it knows what byte offsets to use from the stack pointer, a struct describes the byte offsets to use inside an object reference, and a class is similar.

This makes a lot of sense when your code dynamically links against other modules, since you won't even know what the code definition is until runtime. You can put definitions in your headers, but it takes away your ability to define it dynamically.

Don't think of this as a flaw in C that other languages "fix." Many languages do not expose memory like this, so it's a different programming paradigm completely.

If you're a noobie that's writing C/C++, just stick to the conventions for now. You will appreciate them later on when you have to link against other people's code or are running in unfamiliar runtime environments. It feels arbitrary, but the solution is not to pick up a higher-level language that shields you from memory manipulation unless you didn't need this kind of power in the first place. You can write VMs in Java, but how do you write the JVM?
I (λ (foo) (and (<3 foo) ( T_T foo) (RAGE foo) )) Starcraft
henery
Profile Blog Joined February 2011
Canada89 Posts
December 04 2011 21:04 GMT
#9
As spines said visual studio is your best bet. And you are correct in that object oriented programming is very inefficient in c/++ compared to java. As other people already stated the header is to separate the implementation from the declaration. Once you get used to the extra typing you wont mind it too much. If you write everything in the header it will declare the functions multiple times if you end up using the header multiple times, and give you an error. Your compiler will hate you if you start making nestled classes and you only use a header file.
mmp
Profile Blog Joined April 2009
United States2130 Posts
Last Edited: 2011-12-04 21:10:03
December 04 2011 21:05 GMT
#10
On December 05 2011 05:58 AcrossFiveJulys wrote:
c++ is a total joke for OOP. annoying header files, include loops, and the necessity to overload the copy constructor in many situations and other c++ BS (e.g. manual memory management) has made me switch entirely to java/scala. The java compiler/JVM has come a long way so that compiled c++ isn't really that much more efficient than bytecode anymore, so there's little reason to continue using this language.

Your prejudice is shortsighted. Many languages (Python, Haskell, others) fall back to C/C++ code to get performance. You have an abstraction barrier separating grammar & syntax from what is ultimately machine code any way you look at it.

I believe Java used (or still uses) a C library for SSE operations, and what language is the JVM written in? You cannot talk about performance without respecting the hardware.

Also, try writing a memory allocator in a language that doesn't give you memory access... If you're upset about garbage collection in C, take a look at Go or D.
I (λ (foo) (and (<3 foo) ( T_T foo) (RAGE foo) )) Starcraft
Denar
Profile Blog Joined March 2011
France1633 Posts
December 04 2011 22:40 GMT
#11
You can write function implementations in the .h, but every time you modify something in the function, big parts of the project will be recompiled which will become a huge pain if your project grows.

If it doesn't, and the code won't be shared with other developers, you don't need to care.
AcrossFiveJulys
Profile Blog Joined September 2005
United States3612 Posts
December 04 2011 22:43 GMT
#12
On December 05 2011 06:05 mmp wrote:
Show nested quote +
On December 05 2011 05:58 AcrossFiveJulys wrote:
c++ is a total joke for OOP. annoying header files, include loops, and the necessity to overload the copy constructor in many situations and other c++ BS (e.g. manual memory management) has made me switch entirely to java/scala. The java compiler/JVM has come a long way so that compiled c++ isn't really that much more efficient than bytecode anymore, so there's little reason to continue using this language.

Your prejudice is shortsighted. Many languages (Python, Haskell, others) fall back to C/C++ code to get performance. You have an abstraction barrier separating grammar & syntax from what is ultimately machine code any way you look at it.

I believe Java used (or still uses) a C library for SSE operations, and what language is the JVM written in? You cannot talk about performance without respecting the hardware.

Also, try writing a memory allocator in a language that doesn't give you memory access... If you're upset about garbage collection in C, take a look at Go or D.


Sure, the java compiler might be written in c++, but the point is that with java's JVM and compiler optimizations there is no point in using archaic languages. The abstraction layer is precisely what makes java a more productive language. I have used c, c++, and java extensively and it's amazing how much more productive coding in java is for little performance loss.
munchmunch
Profile Joined October 2010
Canada789 Posts
December 04 2011 23:11 GMT
#13
I don't remember C++ very well (it's been over a decade since I've used it seriously), but in vanilla C you can just stick simple programs in one .c file, no header files required.

By the way, C++ is an incredibly ugly language, but C by itself is very nice. If you just want to learn how to do memory allocation and stuff like that, learn vanilla C.
Slithe
Profile Blog Joined February 2007
United States985 Posts
December 04 2011 23:39 GMT
#14
On December 05 2011 07:43 AcrossFiveJulys wrote:
Show nested quote +
On December 05 2011 06:05 mmp wrote:
On December 05 2011 05:58 AcrossFiveJulys wrote:
c++ is a total joke for OOP. annoying header files, include loops, and the necessity to overload the copy constructor in many situations and other c++ BS (e.g. manual memory management) has made me switch entirely to java/scala. The java compiler/JVM has come a long way so that compiled c++ isn't really that much more efficient than bytecode anymore, so there's little reason to continue using this language.

Your prejudice is shortsighted. Many languages (Python, Haskell, others) fall back to C/C++ code to get performance. You have an abstraction barrier separating grammar & syntax from what is ultimately machine code any way you look at it.

I believe Java used (or still uses) a C library for SSE operations, and what language is the JVM written in? You cannot talk about performance without respecting the hardware.

Also, try writing a memory allocator in a language that doesn't give you memory access... If you're upset about garbage collection in C, take a look at Go or D.


Sure, the java compiler might be written in c++, but the point is that with java's JVM and compiler optimizations there is no point in using archaic languages. The abstraction layer is precisely what makes java a more productive language. I have used c, c++, and java extensively and it's amazing how much more productive coding in java is for little performance loss.


I don't think anyone's going to argue that c/c++ is easier to use than Java. But if you have a particular need for high performance, whether in speed or in memory usage, then you'll still want to resort to c/c++.
CharlieBrownsc
Profile Blog Joined December 2010
Canada598 Posts
December 04 2011 23:41 GMT
#15
Learn C#. The functional aspects are incredibly cool.
SC2 ID: CharlieBrown.318, #1 bitbybit.Prime fan
dartoo
Profile Joined May 2010
India2889 Posts
December 05 2011 00:09 GMT
#16
I think you can visual c++ for free, there should be an express version. I use dot net for work, so the environment was very similar, (and I kinda liked it better after using borland c++ in school )

http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express There ya go, the express version, but if you are a student I think you can get the whole package for free(express is a slightly stripped down version, but will do for most home projects/learning).

C/C++ is considered to be the language you can squeeze a lot of power depending on how good you are with it, and is the language of choice for games etc, but it has a higher learning.
Also one more thing with visual c++ I think there are two versions of all types, one is the framework managed type, and teh non memory managed ones, so you'll have to watch for that (not sure about this, but remember seeing something about it).
rabidch
Profile Joined January 2010
United States20289 Posts
Last Edited: 2011-12-05 00:23:52
December 05 2011 00:22 GMT
#17
i use VS studio for C++. C im usually on a linux system and i use vim or gedit

if you're just doing this for kicks i dont think you'll find it very fun to bother with it, C++ is relatively similar to java, C is worth learning if you want to do low level stuff or fast computing because C works very similar to how the hardware works. C# is more comparable to java as well. anyway you'll probably just be figuring out how to do something in you could do in java in C++/C#, which is not very fun imo. the fun stuff is figuring out how to do things you couldnt do in java

if you want to try something wacky you could always try other style languages, functional or logical like haskell, a lisp fork, prolog...
LiquidDota StaffOnly a true king can play the King.
Bigpet
Profile Joined July 2010
Germany533 Posts
Last Edited: 2011-12-05 04:48:14
December 05 2011 04:41 GMT
#18
On December 05 2011 07:43 AcrossFiveJulys wrote:
Show nested quote +
On December 05 2011 06:05 mmp wrote:
On December 05 2011 05:58 AcrossFiveJulys wrote:
c++ is a total joke for OOP. annoying header files, include loops, and the necessity to overload the copy constructor in many situations and other c++ BS (e.g. manual memory management) has made me switch entirely to java/scala. The java compiler/JVM has come a long way so that compiled c++ isn't really that much more efficient than bytecode anymore, so there's little reason to continue using this language.

Your prejudice is shortsighted. Many languages (Python, Haskell, others) fall back to C/C++ code to get performance. You have an abstraction barrier separating grammar & syntax from what is ultimately machine code any way you look at it.

I believe Java used (or still uses) a C library for SSE operations, and what language is the JVM written in? You cannot talk about performance without respecting the hardware.

Also, try writing a memory allocator in a language that doesn't give you memory access... If you're upset about garbage collection in C, take a look at Go or D.


Sure, the java compiler might be written in c++, but the point is that with java's JVM and compiler optimizations there is no point in using archaic languages. The abstraction layer is precisely what makes java a more productive language. I have used c, c++, and java extensively and it's amazing how much more productive coding in java is for little performance loss.


Well "little performance loss" may hold true for coding non-real-time applications like office-applications but when it comes to real-time and especially games then you'll be clambering for those 5ms.

EA even wrote it's own STL implementation among other things to get better access to the memory allocation because memory fragmentation is going to cost you dearly, especially when you don't have much and you don't run on an out-of-order CPU (so consoles). There's a reason why minecraft for the XBox is not written in Java and it's mainly that you can't tinker with memory management, so no custom allocators.

edit: well ok, on the xbox the other reason may be that there is no access to JVM and using the .NET port would be highly stupid but the better example would be the pocket version for android. You'd think if it's for android then it'd be Java but on a device so limited in memory they had to use C++.
I'm NOT the caster with a similar nick
mrafaeldie12
Profile Joined July 2011
Brazil537 Posts
December 05 2011 04:50 GMT
#19
Well the .h is the declaration and .cpp is the implementation.I use Visual C++ and CodeBlocks.

I have to agree that latest builds of JVM(programmed in C++ of course) are pretty efficient but they will never match the speed of C++.

But Java sure is attractive with its thousands of API s,simple operators and extensive documentation.
"..it all comes thumbling down thumbling down thumblin down"
Please log in or register to reply.
Live Events Refresh
Wardi Open
11:00
#89
IntoTheiNu 1498
WardiTV1111
Rex152
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Ryung 315
LamboSC2 174
Rex 152
ProTech81
StarCraft: Brood War
Calm 3291
Mini 1311
EffOrt 1031
Jaedong 850
Backho 361
Light 300
actioN 243
Leta 190
Soulkey 172
Zeus 158
[ Show more ]
Pusan 77
hero 71
ToSsGirL 63
[sc1f]eonzerg 53
Aegong 45
PianO 39
sSak 27
zelot 20
IntoTheRainbow 17
soO 17
JulyZerg 15
Shine 14
Noble 13
Movie 13
sorry 13
Sacsri 12
ajuk12(nOOB) 6
Icarus 5
Dota 2
qojqva1985
Dendi1790
XaKoH 482
Counter-Strike
byalli1793
kRYSTAL_63
Other Games
singsing3422
B2W.Neo1256
hiko947
Lowko547
Mew2King61
Hui .24
Organizations
Other Games
BasetradeTV190
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
[ Show 16 non-featured ]
StarCraft 2
• StrangeGG 62
• iHatsuTV 19
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• mYiSmile10
• sooper7s
StarCraft: Brood War
• iopq 7
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• WagamamaTV334
League of Legends
• Jankos2799
Upcoming Events
Monday Night Weeklies
2h 9m
Replay Cast
10h 9m
Sparkling Tuna Cup
20h 9m
WardiTV Spring Champion…
21h 9m
Maestros of the Game
1d 1h
The PondCast
1d 20h
Kung Fu Cup
1d 21h
uThermal 2v2 Circuit
2 days
Maestros of the Game
2 days
Replay Cast
2 days
[ Show More ]
Replay Cast
2 days
WardiTV Spring Champion…
2 days
Maestros of the Game
3 days
Replay Cast
3 days
uThermal 2v2 Circuit
4 days
Maestros of the Game
4 days
Replay Cast
4 days
Solar vs Classic
uThermal 2v2 Circuit
5 days
GSL
5 days
herO vs Rogue
Maru vs Cure
uThermal 2v2 Circuit
6 days
BSL
6 days
Replay Cast
6 days
Liquipedia Results

Completed

KK 2v2 League Season 1
RSL Revival: Season 5
Heroes Pulsing #1

Ongoing

BSL Season 22
IPSL Spring 2026
KCM Race Survival 2026 Season 2
Acropolis #4
CSCL: Masked Kings S4
YSL S3
SCTL 2026 Spring
WardiTV Spring 2026
Maestros of the Game 2
2026 GSL S2
Murky Cup 2026
Stake Ranked Episode 2
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026
BLAST Rivals Spring 2026
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026

Upcoming

BSL 22 Non-Korean Championship
CSLAN 4
Blizzard Classic Cup 2026
Kung Fu Cup 2026 Grand Finals
CranK Gathers Season 4: BW vs SC2 Team League
HSC XXIX
uThermal 2v2 2026 Main Event
Heroes Pulsing #3
Heroes Pulsing #2
Esports World Cup 2026
BLAST Bounty Summer 2026
BLAST Bounty Summer Qual
Stake Ranked Episode 3
XSE Pro League 2026
IEM Cologne Major 2026
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.