Thread: Bad practices or good?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    35

    Bad practices or good?

    I wrote a header file with my class that contains static members, Do I have to create a separate file for this class to initilialize these static members? or can i just instantiate them outside the class definition in the header file and will that be good or bad practice?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Defining statics (which is what you are incorrectly referring to as "instantiation" or "initialization") in a header file is bad practice more often than not.

    If the header file is #include'd by more than one source file, then the definition of that static member usually needs to be in a separate source file. Otherwise, the one-definition rule is violated. Practically that usually results in a linker error, without creating an executable.

    There are some tricks that allow you to get away with placing the definition of a class static in a header file, but they usually involve coercing the compiler to create a definition in only one object file. Compilers don't do that by default.

    If the header file is only #include'd by one source file, it makes no difference. However, the purpose of placing declarations in a header file is usually to allow those declarations to be used by more than one distinct source file.
    Last edited by grumpy; 03-04-2013 at 02:03 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    35
    Quote Originally Posted by grumpy View Post
    Defining statics (which is what you are incorrectly referring to as "instantiation" or "initialization") in a header file is bad practice more often than not.

    If the header file is #include'd by more than one source file, then the definition of that static member usually needs to be in a separate source file. Otherwise, the one-definition rule is violated. Practically that usually results in a linker error, without creating an executable.

    There are some tricks that allow you to get away with placing the definition of a class static in a header file, but they usually involve coercing the compiler to create a definition in only one object file. Compilers don't do that by default.

    If the header file is only #include'd by one source file, it makes no difference. However, the purpose of placing declarations in a header file is usually to allow those declarations to be used by more than one distinct source file.

    How would you go about initializing the class members in a separate implementation file and then including that in the main source file? So for example I have my class declarations in the header file then in my implementation file I include that header file and initialize the members of that class using the :: operator, How would I go about linking those two together in my main source file? would I have to include my cpp file after I include the header file?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Compile all .cpp files separately, then link them. How to do that depends on your compiler/IDE.
    Typically, the process is as follows:
    - To do it in an IDE, just add all .cpp files to your project and build it.
    - With a compiler, you specify all .cpp files for compiling with appropriate flags, for example: g++ a.cpp b.cpp -o out.exe (don't recall the exact flags for g++, be warned...)

    Never include .cpp files.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    g++ a.cpp b.cpp -o out.exe (don't recall the exact flags for g++, be warned...)
    that command will work just fine.

    if you want the compiler to be a bit more verbose about what you're doing wrong (nobody writes perfect code - not even elysia ) you can add -Wall and -Wextra, or even -pedantic. you can even add -Werror if you want warnings to be treated as errors. if you want an easier time debugging with gdb, add the -g option. this is, of course, assuming you're using G++. if you're using a different compiler, you'll need to research the options, as I am most familiar with G++, and my knowledge is pretty weak on all the others.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best practices when coding
    By c_weed in forum Tech Board
    Replies: 8
    Last Post: 04-23-2012, 10:06 PM
  2. getchar() best practices
    By albundy in forum C Programming
    Replies: 2
    Last Post: 09-03-2011, 05:55 AM
  3. Good Programming Practices
    By mark909 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2011, 03:57 PM
  4. deque best practices
    By George2 in forum C++ Programming
    Replies: 10
    Last Post: 03-02-2008, 08:11 PM
  5. read-only data members. A question of good practices
    By Mario F. in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2006, 04:35 AM