Thread: Function Declaration

  1. #1
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    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!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    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!

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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
    28,413
    Actually, someVar2 is initialized to 0, but we know what you mean.
    Hope leeor_net does though
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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, 01:49 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01: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, 07:18 AM