Sign in to follow this  

Can I use only .cpp files and no .h files?

This topic is 4859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I like Java and C# because I have no need of splitting my classes in two files (.cpp and .h). Question is, can code in C++ using only .cpp files and including .cpp files directly? I know that .h files are needed to prototype libraries but this is not the case. Can I avoid header files or is a bad practice?

Share this post


Link to post
Share on other sites
Well technically you could, but it wouldn't be practical. You'd have to copy n' paste prototypes into the files that need them, making the files much larger than they need to be. It'd be best to just bite the bullet and use .h files, as you won't gain anything from not using them--other than a slight decrease in compile times.

Share this post


Link to post
Share on other sites
Quote:
Original post by bytecoder
Quote:

Long answer - yes.

Talk about contradiction...

Not at all! The short answer is no, it's 2 letters short.
the long answer is yes, it's 3 letters long.
Quote:
Original post by Fruny
I see we agree. Perfect.

Indeed!

Now, for an actual answer:

extern "C" { typedef struct tagPOINT { long x; long y; } POINT; typedef struct tagMSG { void* hwnd; unsigned int message; unsigned int wParam; long lParam; unsigned long time; POINT pt; } MSG; typedef long (__stdcall* WNDPROC) (void*, unsigned int, unsigned int, long); typedef struct { unsigned int style; WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; void* hInstance; void* hIcon; void* hCursor; void* hbrBackground; const char* lpszMenuName; const char* lpszClassName; } WNDCLASSA; void __stdcall PostQuitMessage(int); long __stdcall DefWindowProcA(void*, unsigned int, unsigned int, long); unsigned short __stdcall RegisterClassA(const WNDCLASSA*); void* __stdcall CreateWindowExA(unsigned long, const char*, const char*, unsigned long, int, int, int, int, void*, void*, void*, void*); int __stdcall GetMessageA(MSG*, void*, unsigned int, unsigned int); int __stdcall TranslateMessage(MSG*); int __stdcall DispatchMessageA(MSG*); long __stdcall WndProc(void* handle, unsigned int msg, unsigned int w, long l) { switch(msg) { case 0x0002: PostQuitMessage(0); } return DefWindowProcA(handle, msg, w, l); } int __stdcall ShowWindow(void*, int); int __stdcall UpdateWindow(void*); int __stdcall WinMain(void* h,void*, char*, int) { WNDCLASSA wc = { 0x0002 | 0x0001, WndProc, 0, 0, h, 0, 0, 0, 0, "a" }; RegisterClassA(&wc); void* v = CreateWindowExA(0, "a", "A", 0x00C00000L | 0x00080000L | 0x00040000L | 0x00020000L | 0x00010000L, 0, 0, 300, 300, 0, 0, h, 0); UpdateWindow(v); ShowWindow(v, 5); MSG msg; while(GetMessageA(&msg, 0, 0, 0)) { TranslateMessage(&msg); DispatchMessageA(&msg); } return 0; } }


Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Quote:
Original post by Washu
Now, for an actual answer:
*** Source Snippet Removed ***


I shall name it answer.cpp.

Perhaps we should call it onelineanswer.cpp?

Share this post


Link to post
Share on other sites
Anyway, a .h file is just a .cpp file tailored specifically for inclusion. #includes are simple copy-paste. You can write your code so that everything is already included in the .cpp file (as Washu has done), but that won't probably do what you desire -- the C++ compilation model is different than that of Java and C#.

Ergo, both Washu and my answer are correct, and there is no contradiction.

Share this post


Link to post
Share on other sites
Quote:

Ergo, both Washu and my answer are correct, and there is no contradiction.

If you're refering to my previous post, I was talking about the fact that yes is a short answer, atleast to me, when Washu said it was a long one.

Share this post


Link to post
Share on other sites
Quote:
Original post by bytecoder
Quote:

Ergo, both Washu and my answer are correct, and there is no contradiction.

If you're refering to my previous post, I was talking about the fact that yes is a short answer, atleast to me, when Washu said it was a long one.


But it is a long answer, just because you can't see it doesn't mean it's not there *hint hint*

Share this post


Link to post
Share on other sites

This topic is 4859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this