CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Confused about constructor initializations.

Hybrid View

  1. #1
    Join Date
    Nov 2010
    Location
    CA
    Posts
    29

    Confused about constructor initializations.

    I encountered the following while reading some C++ code.
    Consider these two code examples:

    Code:
    class Foo {          // Declares class Foo
    public:
        int x;           // Member variable
     
        Foo(): x(0) {    // Constructor for Foo,
        }                //  initializes x
     
        int bar(int i) { // Member function bar()
            return 3*i + x;
        }
    };
    Code:
    class Foo {          // Declares class Foo
    public:
        int x;           // Member variable
     
        Foo() {    // Constructor for Foo,
            x(0);    // initializes x
        }
     
        int bar(int i) { // Member function bar()
            return 3*i + x;
        }
    };
    I don't know what the difference is between how x is initialized in each.
    Are they the same thing, just multiple ways to do it?

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Confused about constructor initializations.

    Quote Originally Posted by jsg View Post
    Code:
       Foo() {    // Constructor for Foo,
            x(0);    // initializes x
        }
    That's not valid code. You can do x = 0; in the c'tor body.
    For the difference between the two methods, see http://www.parashift.com/c++-faq-lit....html#faq-10.6
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Nov 2010
    Location
    CA
    Posts
    29

    Re: Confused about constructor initializations.

    Quote Originally Posted by D_Drmmr View Post
    That's not valid code. You can do x = 0; in the c'tor body.
    For the difference between the two methods, see http://www.parashift.com/c++-faq-lit....html#faq-10.6
    Yes, I meant to do x = 0; in the second example.

    That article helped somewhat, I don't understand everything, but I do get the gist that initialization list are better to use than assignment in most cases.

  4. #4
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Confused about constructor initializations.

    If the field was 'SomeType x' instead of 'int x', and SomeType didn't have a no-parameter constructor, then your only choice would be the 1st way.

  5. #5
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Confused about constructor initializations.

    Quote Originally Posted by jsg View Post
    Code:
        Foo() {    // Constructor for Foo,
            x(0);    // initializes x
        }
    Quote Originally Posted by D_Drmmr View Post
    That's not valid code.
    Incorrect, it is valid code, it just isn't doing what the OP expected. That is calling a method named x.

    You are not allowed to do code like that because if it was allowed, callback functions would be unable to work. You can only use that syntax in very select cases. Constructor initialization lists is one of those cases.
    Last edited by ninja9578; November 30th, 2010 at 02:18 PM.

  6. #6
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Confused about constructor initializations.

    Quote Originally Posted by ninja9578 View Post
    Incorrect, it is valid code, it just isn't doing what the OP expected. That is calling a method named x.

    You are not allowed to do code like that because if it was allowed, callback functions would be unable to work. You can only use that syntax in very select cases. Constructor initialization lists is one of those cases.
    Actually in the context of the code written by the OP, D_Drmmr is correct, the code is invalid - it will not compile even if there is a function named x declared prior to Foo, because due to look up rules the compiler will try to match x(0) against the member variable first and since an int does not define operator()(int), the compilation will fail.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)