Bool initialization in class

This is a discussion on Bool initialization in class within the C++ Programming forums, part of the General Programming Boards category; Hey! I've got a class with a private bool isScanning, and I'm trying to set it to false when initializing ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    118

    Bool initialization in class

    Hey!

    I've got a class with a private bool isScanning, and I'm trying to set it to false when initializing it in the class:

    Code:
    class blah
    {
        public:
            function1();
            function2();
        private:
            bool isScanning = false;
    };
    I feel so stupid for not knowing why, but why would GCC give me:

    ISO C++ forbids initialization of member `isScanning'
    I could have sworn you were allowed to set a value for variables in classes, no? I know I've done it in the past...

    Thanks!
    FlyingIsFun1217

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Initialize it in the constructor.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Alright, but... why can't you set the value in the constructor in a class? Seems kinda stupid, I'm hoping I'm missing something bigger here...

    FlyingIsFun1217

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because you don't have a variable yet to initialize. (Not until you do MyClass foo; do you have a variable.)

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    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

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    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.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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
    To be honest, I have very little idea what you think you mean here, but I'll try to answer all the same. When you declare a class, you create a grand total of nothing. You say, "IF someone decides they want a blah, give 'em one of these, and one of those, and a couple of that." If you want each of your objects to start life with isScanning equal to false, then you need to set that when your object is created -- in the constructor. That's what the constructor is for, after all, to set up each of your objects the way you want.

  8. #8
    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

  9. #9
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    I think that we just have a terminology clash here. The OP has probably seen code like the following before, thinking that the variables are initialised "inside the class" without realising that nothing actually gets initialised until instances are created.

    Code:
    #include <iostream>
    
    class A
    {
    private:
    	int x;
    
    public:
    	A () : x (10) {}
    	int getval () {return x;}
    };
    
    class B
    {
    private:
    	int y;
    public:
    	B () {y = 20;}
    	int getval () {return y;}
    };
    
    int main (void)
    {
    	A instance_of_A;
    	B instance_of_B;
    
    	std::cout << instance_of_A.getval() << std::endl << instance_of_B.getval() << std::endl;
    
    	return 0;
    }
    For a casual observer, the above code might make it look like the initialisation occurs inside the class definition. This is most definitely not the case.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    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.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    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, 11:05 AM
  5. How do I play an MP3?
    By Hunter2 in forum Windows Programming
    Replies: 28
    Last Post: 05-20-2002, 08:49 PM

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