is it bad practice to....

This is a discussion on is it bad practice to.... within the C++ Programming forums, part of the General Programming Boards category; ....define functions inside a class, like this Code: class Blah { public: Blah() { // ..... } }; rather than ...

  1. #1
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369

    is it bad practice to....

    ....define functions inside a class, like this

    Code:
    class Blah
    {
    public:
         Blah()
         {
               // .....
         }
    };
    rather than like this
    Code:
    class Blah
    {
    public:
         Blah();
    };
    
    Blah::Blah()
    {
         // .....
    }
    ???
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    The effect is different. If you define a method inside the class then the method is taken to be inline, if you define it outside of the class then you have the option of making it inline if you want, but it isn't by default. As for good and bad practice, it doesn't really effect readability or do anything really bad, so no worries either way :-)
    *Cela*

  3. #3
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Thanks!
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  4. #4
    ! |-| /-\ +3 1337 Yawgmoth's Avatar
    Join Date
    Dec 2002
    Posts
    187
    What's the difference between inline and noninline functions?
    L33t sp3@k sux0rz (uZ it t@k3s 10 m1|\|ut3s 2 tr@nzl@te 1 \/\/0rd & th3n j00 h@\/3 2 g3t p@$t d@ m1zpelli|\|gz, @tr0(i0u$ gr@mm@r @|\|d 1n(0/\/\pr3#3|\|$1bl3 $l@|\|g. 1t p\/\/33nz j00!!

    Speling is my faverit sujekt

    I am a signature virus. Add me to your signature so that I may multiply.

  5. #5
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    as far as OOP goes I would say defining the class defininitions in a .h file
    and the implementation in another file is better then grouping it all together.

    in other words it is good to separate interface from implementation.

    so i would do

    Blah.h
    Code:
    class Blah
    {
    public:
         Blah();
    };

    and
    Blah.cc

    Code:
    #include"Blah.h"
    
    Blah::Blah()
    {
         // .....
    }

    This may also speed up you compilation a little bit

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Inline functions are expanded macro-like every time they are called thus
    Code:
    class myclass {
       int *data;
    public:
       myclass(int size=10) : data(new int[size]) {};
       int & operator [] (int n) {return data[n];}
       ~myclass() { delete [] data;}
    };
    
    int main() {
      myclass a;
      a[5] = a[3] + a[2];
      return 0;
    }
    //is compiled as:
      myclass a;
      a.data = new int[10];  // mild magic here
      a.data[5] = a.data[3] + a.data[2]; 
      delete [] data; // more magic
      return 0;
    The upside is that you don't pay overhead for function calls, the downside is that your programs get bigger and compilation takes longer.

  7. #7
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Originally posted by beege31337
    as far as OOP goes I would say defining the class defininitions in a .h file
    and the implementation in another file is better then grouping it all together.

    in other words it is good to separate interface from implementation.
    Well if the source file isn't too long then I see nothing wrong with having the class declarations and everything else being in the source file as well. Same goes if you are posting the code on a board such as this one. Also how would that speed up compilation? It would need to open the header file also, seems to me it would be slower...

  8. #8
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    Originally posted by Munkey01
    Also how would that speed up compilation? It would need to open the header file also, seems to me it would be slower...
    I was saying that abt. the possibility of speeding compilation because if you have everything in one file, everytime you compile ( or more specifically make ) your program, everything is recompiled.

    but if you have the class definintions separate from the classes ( in a .h ) then the implementation of the class ( which is compiled into an object file ) doesn't have to be recompiled everytime another program uses the class.
    If you have the definitions and implementation grouped into the same file then everytime you use the class it must be recompiled. One of the reasons we have .h files is to prevent this need for recompilation.

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. Replies: 11
    Last Post: 11-13-2002, 12:29 PM
  3. Bad Practice??
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 07:37 AM
  4. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 07:31 AM
  5. Bad code or bad compiler?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 10-22-2001, 09:08 PM

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