Is having implementation in headers illegal, or just bad practice?

This is a discussion on Is having implementation in headers illegal, or just bad practice? within the C++ Programming forums, part of the General Programming Boards category; I am defining data structures for a project. i have some simple implementation that i want in my header because ...

  1. #1
    Shadow12345
    Guest

    Is having implementation in headers illegal, or just bad practice?

    I am defining data structures for a project. i have some simple implementation that i want in my header because i don't want to break it up into separate files. Is this illegal or bad practice or what?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    > Is having implementation in headers illegal, or just bad practice?
    It's best to avoid it if you can, but not illegal.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Shadow12345
    Guest
    well i guess it is illegal, i am too quick to make a post before actually trying something myself.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    template functions can be a good idea to put in a header. Most compilers cannot handle the export keyword, which makes template functions in source files possible.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    It can cause your link to fail as multiple definitions of the functions you define will exist in more than 1 module.....

    You can use the keyword "static" to give your function internal linkage (relative only to the module that uses it)...but that is deprecated and these days you should just include the function in an empty namespace

    Code:
    //main.cpp
    #include "temp.h"
    
    int main(){
    
    	foo();
    	
    	bar();
    
    }
    Code:
    //module.cpp
    #include "temp.h"
    
    void foo(){bar();}//definition of foo (in cpp file)
    Code:
    //temp.h
    #include <iostream>
    
    void foo();//forward declaration for foo
    
    namespace{//empty namespace
    	void bar(){//definition of foo (in h file)
    		std::cout << "Foobar" << std::endl;
    	}
    }

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    inline functions in the header are fine though.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I would only use an inline in the header when you intend to make a function inline for speed reasons. inline is after all something of a glorified macro.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I'm not sure, but I've once heard that if you define a function in the header file, it's automaticaly inlined...So is that true?

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You mean anonymous namespace fordy not empty namespace.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    There is never any reason to include code in a header file. If you must inline then specify with inline and let compiler decide what to do and put the implementation in a .cpp file and keep your headers for declarations only.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    stoned, that's good practice so long as the inline function is used in the module where it's defined. you can't use an inline function in another module however.

    edit: Module == cpp file just to clarify
    Last edited by FillYourBrain; 11-13-2002 at 11:28 AM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Originally posted by Stoned_Coder
    You mean anonymous namespace fordy not empty namespace.
    LOL...yup....your right...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bad practice or acceptable?
    By Noose in forum C++ Programming
    Replies: 6
    Last Post: 06-09-2004, 01:43 AM
  2. is it bad practice to....
    By abrege in forum C++ Programming
    Replies: 7
    Last Post: 02-07-2003, 08:03 PM
  3. Ternary operators, bad practice?
    By PJYelton in forum C++ Programming
    Replies: 34
    Last Post: 12-21-2002, 07:10 AM
  4. Shocking(kind of)
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 12-10-2002, 07:52 PM
  5. Bad Practice??
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 07:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21