One line functions in headers, bad?

This is a discussion on One line functions in headers, bad? within the C++ Programming forums, part of the General Programming Boards category; A class: Code: struct pos{ int x; int y; int z; pos(){} pos(int a, int b, int c) {x = ...

  1. #1
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587

    One line functions in headers, bad?

    A class:
    Code:
    struct pos{
        int x;
        int y;
        int z;
        pos(){}
        pos(int a, int b, int c){x = a;y = b;z = c;}
        pos(const pos& p){x = p.x;y = p.y;z = p.z;}
        pos operator=(pos p){x = p.x;y = p.y;z = p.z;}
        bool operator==(pos p) {return bool((x == p.x) && (y == p.y) && (z == p.z));}
    };
    Is it wrong to do that? I'm working on my first sizable C++ project and I'm doing this for most small functions to save clutter in my .cpp files.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,000
    The more you do that, the harder it becomes to read it.
    Usually, IDEs support outlining. If you want to avoid clutter, I would suggest using that instead.
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    I would suggest:
    Code:
    struct pos {
        int x;
        int y;
        int z;
    
        pos() {} // perhaps you should zero initialise the member variables?
    
        pos(int a, int b, int c) : x(a), y(b), z(c) {}
    
        bool operator==(const pos& p) const
        {
            return (x == p.x) && (y == p.y) && (z == p.z);
        }
    };
    You might also consider if the member variables should actually be private.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Initialize them to what? They'll just be 0, right?

    Code:
    pos(int a, int b, int c) : x(a), y(b), z(c) {}
    Whoa... What's that called?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,000
    Quote Originally Posted by User Name: View Post
    Initialize them to what? They'll just be 0, right?
    No.

    Code:
    pos(int a, int b, int c) : x(a), y(b), z(c) {}
    Whoa... What's that called?
    Initializer list.
    Effectively calls the constructor instead of assignment operator. Has little to no effect on built-in types, but on classes and complex types it can be night or day.
    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.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Quote Originally Posted by User Name:
    Initialize them to what? They'll just be 0, right?
    If you do not initialise them in the constructor, they may well have "garbage" values when the object is constructed.

    Quote Originally Posted by User Name:
    Whoa... What's that called?
    Constructor and its initialisation list.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Code:
    pos() : x(0), y(0), z(0) {}
    Would that work?

    EDIT: Nevermind, it works.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Quote Originally Posted by User Name: View Post
    Code:
    pos() : x(0), y(0), z(0) {}
    Would that work?

    EDIT: Nevermind, it works.
    And... just another one to throw you for a loop, you could use default arguments to eliminate one of those constructors so you'd have just one doing the job of both:

    Code:
    struct pos {
        int x;
        int y;
        int z;
    
        pos(int a = 0, int b = 0, int c = 0) : x(a), y(b), z(c) {}
    
    };
    
    ...
    
    pos foo1;          // Creates default pos object with x/y/z initialized to 0
    pos foo2(1,2,3);   // Create pos object with x/y/z initialized to 1/2/3
    pos foo3(4,5);     // Create pos object with x/y/z initialized to 4/5/0
    pos foo4(6);       // Create pos object with x/y/z initialized to 6/0/0
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    One catch with hk_mp5kpdw's suggestion is that you can then write:
    Code:
    pos foo5 = 7;
    unless you declare that constructor as explicit.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Code:
    struct pos_t{
    	int x, y, z;
    
    	pos_t(int a) : x(a), y(a), z(a) {}
    	explicit pos_t(int a = 0, int b = 0, int c = 0) : x(a), y(b), z(c) {}
    };
    Which would be used with pos_t(1)?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,000
    Why not try it?
    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.

  12. #12
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Sorry. I wasn't close to being able to debug at the time.

    call of overloaded ‘pos_t(int)’ is ambiguous

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,000
    Yes, that's what I figured. Your first constructor is redundant.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to do encryption in C
    By sankarv in forum C Programming
    Replies: 33
    Last Post: 12-28-2010, 11:01 AM
  2. can't compile library
    By pjs111 in forum C Programming
    Replies: 16
    Last Post: 05-24-2010, 02:42 PM
  3. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 11:04 AM
  4. Clearing single line?
    By louieansonng in forum C Programming
    Replies: 14
    Last Post: 07-27-2009, 12:10 AM
  5. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM

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