Thread: Bool initialization in class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by FlyingIsFun1217 View Post
    But isn't it being initialized when it goes to compile the private class variables? Or does that only serve as a 'pointer' for future reference (in the code), and it's actually initialized when first used in other functions?

    FlyingIsFun1217
    There is little wrong with the syntax you use, except that it is not valid C++. Perhaps the designers of the language did not want there to be ambiguity if an object was initialized both as you suggest and in the initializer list of a constructor. Or perhaps they simply did not think of that syntax.

    But an object is initialized when you call it's constructor. In this way different objects can be initialized differently, depending on constructor arguments, and each instance has it's own copy of the variables.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Quote Originally Posted by King Mir View Post
    There is little wrong with the syntax you use, except that it is not valid C++.
    Interesting... so it would be fine had it been designed that way/the compiler was made to support that? Seems like one of those things that should have been done.

    FlyingIsFun1217

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by FlyingIsFun1217 View Post
    Interesting... so it would be fine had it been designed that way/the compiler was made to support that? Seems like one of those things that should have been done.

    FlyingIsFun1217
    It is done that way in Java. Richie T demonstrates the equivalent C++ syntax.

    The problem with the Java syntax is that it does not allow initialization to depend on constructor arguments.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by King Mir View Post
    It is done that way in Java. Richie T demonstrates the equivalent C++ syntax.

    The problem with the Java syntax is that it does not allow initialization to depend on constructor arguments.
    That's the kicker. E.g. for a copy-constructor you would most likely not want an int default initialised to zero (it oyu had written that) before being given the value in the item it is being constructed from.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  5. How do I play an MP3?
    By Hunter2 in forum Windows Programming
    Replies: 28
    Last Post: 05-20-2002, 08:49 PM