Function Declaration

This is a discussion on Function Declaration within the C++ Programming forums, part of the General Programming Boards category; Hello, everyone. I'm sure this question has an easy answer but I'm not even sure where to begin. I'm working ...

  1. #1
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    Posts
    147

    Question Function Declaration

    Hello, everyone. I'm sure this question has an easy answer but I'm not even sure where to begin.

    I'm working with source code that does the following:

    Code:
    someFunc( arg1, arg2 ) : someVar1( arg1 ), someVar2 ( 0 )
    {
       ...
    }
    What is happening after the argument list? What are the benefits?

    Thanks for your help!
    - Leeor

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,410
    That is a constructor initialisation list. Basically, it initialises the member variables someVar1 and someVar2 with arg1 and arg2 respectively. The benefits is that initialisation is cheaper than default initialisation followed by assignment, and in some cases (e.g., const and reference member variables) you cannot assign at all.

    EDIT:
    in the case of member variables that are references, you can assign, but you cannot construct. As such, it has to be initialised with something before the constructor body is entered.

    Oh, and look below for another mistake, heheh.
    Last edited by laserlight; 09-05-2007 at 12:34 PM.
    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

  3. #3
    Professional Chef leeor_net's Avatar
    Join Date
    Apr 2004
    Location
    Charles Town, WV
    Posts
    147
    Thank you for your response!

    I thought it was something like that... I had just never seen it in practice before (note, I'm a hobbyist programmer... :P )

    So thanks again for your help! The code makes that much more sense now!
    - Leeor

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Basically, it initialises the member variables someVar1 and someVar2 with arg1 and arg2 respectively.
    Actually, someVar2 is initialized to 0, but we know what you mean.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,410
    Actually, someVar2 is initialized to 0, but we know what you mean.
    Hope leeor_net does though
    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

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> I had just never seen it in practice before

    In reality, you should almost always use the initializer list to intialize member variables. It is unfortunate that many books and tutorials fail to teach this habit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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