Thread: Non Static Cls Variable Initialization?

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    40

    Non Static Cls Variable Initialization?

    If I put all my class vars in the public section of the Class, everything is cool until I try to add initialization values to them. Then I get a,
    [Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11

    So then in an effort to stay compatible to earlier standards I move all the vars to the Ctor. But when I compile I get
    [Error] 'WhateverVar' was not declared in this scope.
    (when WhateverVar is used in a class function)

    So in essence (unless I've missed something) the new standard requires you put non-static class vars both in the class declaration and in the Ctor if you want to initialize them and keep class scope of them in funcions.
    Would anyone give a novice like me some insight into the reasoning behind this ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think it would help if you provided a concrete code example. For example, this is valid in C++11+:
    Code:
    class X
    {
    public:
        int get() const
        {
            return n;
        }
    private:
        int n = 123;
    };
    It is not valid pre-C++11. If you need to be compatible with compiling with respect to pre-C++11, you would write:
    Code:
    class X
    {
    public:
        X() : n(123) {}
    
        int get() const
        {
            return n;
        }
    private:
        int n;
    };
    The above of course works in C++11+ too. So, what exactly is the problem that you are facing?

    EDIT:
    Oh, reading your post again, my guess is that you did this:
    Code:
    class X
    {
    public:
        X()
        {
            int n = 123;
        }
    
        int get() const
        {
            return n;
        }
    };
    This is an error in standard C++, regardless of which edition of the standard. It is an error because n is declared as a non-static local variable of the constructor, so its scope and hence its lifetime is limited to the scope of the constructor (not the lifetime of the object). Therefore, you cannot refer to n in the get member function because n has not been declared in that scope. This error is entirely due to a mistake on your part; it is has nothing to do with changes introduced in C++11.
    Last edited by laserlight; 07-09-2016 at 12:53 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
    May 2016
    Posts
    40
    Quote Originally Posted by laserlight View Post
    . . . . . . . .
    . . . . EDIT:
    Oh, reading your post again, my guess is that you did this:
    Code:
    class X
    {
    public:
        X()
        {
            int n = 123;
        }
    
        int get() const
        {
            return n;
        }
    };
    This is an error in standard C++, regardless of which edition of the standard. It is an error because n is declared as a non-static local variable of the constructor, so its scope and hence its lifetime is limited to the scope of the constructor (not the lifetime of the object). Therefore, you cannot refer to n in the get member function because n has not been declared in that scope. This error is entirely due to a mistake on your part; it is has nothing to do with changes introduced in C++11.
    Yes that's exactly what I did. So in pre Cpp 11, I must use the ctor initialize list and not plain assignment. So if I have public class member, would I initialize it like this?

    Code:
    class X
    {
    public:
        int n;
        X() : n(123) {}
     
        int get() const
        {
            return n;
        }
    private:
        // none
    };
    And another question, is the public var n an instance var or a class var ? If I understand it correctly an instance var has it's own value (copy) for every object of that class. Whereas a class var is the same var for every object of that class (?)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by R_W_B
    So if I have public class member, would I initialize it like this?
    Access control makes no difference here, so whether it is private, protected, or public, you would initialise it in the same way.

    Quote Originally Posted by R_W_B
    is the public var n an instance var or a class var ?
    Access control makes no difference here, so a non-static member variable is an "instance variable", whether it is private, protected, or public, and likewise a static member variable is a "class variable", whether it is private, protected, or public.

    Quote Originally Posted by R_W_B
    If I understand it correctly an instance var has it's own value (copy) for every object of that class. Whereas a class var is the same var for every object of that class (?)
    Yes.
    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

  5. #5
    Registered User
    Join Date
    May 2016
    Posts
    40
    Quote Originally Posted by laserlight View Post
    Access control makes no difference here, so whether it is private, protected, or public, you would initialise it in the same way.
    . . . . . . . . . . . . . . a non-static member variable is an "instance variable", whether it is private, protected, or public, and likewise a static member variable is a "class variable", whether it is private, protected, or public. . . . . . . . .
    Thanks you've cleared confusion in many adjacent facets. And on the declare/assignment specifics, if I understand correctly now it is a mute issue whether I use the Ctor initializing list OR whether I use common assignment in the Ctor.

    The fact is for all specifications, I must declare the var in the class body to give it class scope. And if I need to initialize it in pre C++11, then it must be done in the Ctor in some way.

    My only remaining question(s),
    What was the reasoning of pre C++11 to not allow initialization within the Class body (outside the Ctor).

    And if the first answer does not also explain the second,
    What was the reasoning of pre C++11 to allow initialization along with the declaration in the Class Body.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by R_W_B
    And on the declare/assignment specifics, if I understand correctly now it is a mute issue whether I use the Ctor initializing list OR whether I use common assignment in the Ctor.
    No, that is not true. Although both are "assignment" when used in a more general computer science sense, initialisation and assignment in C++ are different concepts. By the time control enters the body of the constructor, the member variables will have been initialised, or have been created without a specific initial value: you would only be able to assign to them.

    Quote Originally Posted by R_W_B
    What was the reasoning of pre C++11 to not allow initialization within the Class body (outside the Ctor).
    I don't know, but it is quite possible that the reason is simply: we didn't think of it at the time, since such syntax was only permitted for static const integer members, and the semantics are somewhat different. I have never read of pre-C++98 discussion that raised this possibility but rejected it.

    Quote Originally Posted by R_W_B
    What was the reasoning of pre C++11 to allow initialization along with the declaration in the Class Body.
    By "declaration in the Class Body" I presume you mean the definition of a constructor. This was obviously done so that you can use the constructor to initialise member variables.
    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

  7. #7
    Registered User
    Join Date
    May 2016
    Posts
    40
    Quote Originally Posted by laserlight View Post
    No, that is not true. Although both are "assignment" when used in a more general computer science sense, initialisation and assignment in C++ are different concepts. By the time control enters the body of the constructor, the member variables will have been initialised, or have been created without a specific initial value: you would only be able to assign to them.
    Oh ok but intialized to whatever the compiler does (or does not) on it's own is not what I meant by the terms. But I understand you terminology.

    Quote Originally Posted by laserlight View Post
    I don't know, but it is quite possible that the reason is simply: we didn't think of it at the time, since such syntax was only permitted for static const integer members, and the semantics are somewhat different. I have never read of pre-C++98 discussion that raised this possibility but rejected it.
    Ok thanks.

    Quote Originally Posted by laserlight View Post
    By "declaration in the Class Body" I presume you mean the definition of a constructor. This was obviously done so that you can use the constructor to initialise member variables.
    No, not at all. By declaration I meant the declaration of the variable happening in the class declaration not the definition in the Ctor.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, then instead of "pre-C++11" you meant "C++11". The reasoning is presumably to simplify cases where a non-static member variable has a consistent initial value, but there are multiple user defined constructors.
    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

  9. #9
    Registered User
    Join Date
    May 2016
    Posts
    40
    Quote Originally Posted by laserlight View Post
    Oh, then instead of "pre-C++11" you meant "C++11". The reasoning is presumably to simplify cases where a non-static member variable has a consistent initial value, but there are multiple user defined constructors.
    Yes that is correct, I had the question correct in the first part but in my second question I got that mixed.

    I've never had the need to have a static variable or I would probably understand the reasoning of the "pre-C++11 better.

    At my point in learning it seems that a separate copy of the class vars for each object is the function I'd want. But the fact that C++11 now allows assignment outside the Ctor just made me wonder why it wasn't always that way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static variable initialization
    By sjmp in forum C Programming
    Replies: 9
    Last Post: 06-12-2013, 03:43 PM
  2. static variable initialization
    By Saurabh Mehta in forum C Programming
    Replies: 8
    Last Post: 11-21-2012, 02:33 PM
  3. Why initialization of static member is a must?
    By meili100 in forum C++ Programming
    Replies: 9
    Last Post: 04-11-2008, 05:22 PM
  4. Replies: 6
    Last Post: 12-13-2007, 08:20 PM
  5. Order of initialization of static variables
    By Sang-drax in forum C++ Programming
    Replies: 4
    Last Post: 06-20-2004, 04:31 PM

Tags for this Thread