Thread: Please help understand following c++ syntax

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    Please help understand following c++ syntax

    Following is the piece of code:

    Code:
    ProcessState::ProcessState() : mDriverFD(open_driver())
    1. It is constructor.
    2. What I am not able to get is the addition of the following syntax " : mDriverFD(open_driver())" what does it mean? What does the addition of the following " : mDriverFD(open_driver())" mean to the statement?

    Thanks in Advance

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    It's an initialization list. From there, you can set values to your class's variables/constants or even call a specific constructor of a standard or otherwise class.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Thanks......

    I am in studying phase..

    As I am aware of following intialization method:
    ex:
    Code:
    class A{   
    int a;
    public:
           A(int b){a=b;}
    }
    Probaly an brief example would clarify my mind

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    This :
    Code:
    class A{   
    int a;
    public:
           A(int b){a=b;}
    }
    is the same as:
    Code:
    class A{   
    int a;
    public:
           A(int b): a(b) {}
    }
    But if you're using something other than the built in types:

    This :
    Code:
    class A{   
    string a;
    public:
           A(string b){a=b;}
    }
    is slower than:
    Code:
    class A{   
    string a;
    public:
           A(string b): a(b) {}
    }
    Because the first firstly calls the default constructor and then sets a=b, while the second directly calls the appropriate constructor.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It is also easier to achieve exception safety with an initialiser list, as the compiler does the work of cleaning things up properly if an exception is thrown.
    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.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Thanks for the explanation........

    Can you point me to any good link or suggest books which explains similar kind of syntax's .....As i don't see these explanations in regular books.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To be fair,
    Code:
    class A{   
    int a;
    public:
           A(int b) { a = 0; }
    }
    Tells the compiler to
    1) Call the default constructor for a (which does not exist),
    2) Assign 0 to a.

    Code:
    class A{   
    int a;
    public:
           A(int b): a(b) {}
    }
    This tells the compiler to call the constructor for a that takes an int.

    So they're different, while the later being more efficient for custom types, and sometimes also a requirement (such as when there is no default constructor available).
    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. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM