Initialization of data members

Discussion in 'C++' started by Dave Theese, Sep 16, 2003.

  1. Dave Theese

    Dave Theese Guest

    Hello all,

    In the code below, why does a syntax error appear at the location commented
    "Syntax error!!!"? What language rule is coming into play here?

    The example below also shows an alternative (use of an initializer list) to
    the illegal syntax I attempted to use, but I'm still left wondering why I
    could not do what I attempted...

    Thanks,
    Dave

    #include <iostream>
    #include <vector>

    using namespace std;

    class foo
    {
    public:
    foo(): a(10) {for (int i = 0; i != a.size(); ++i) cout << a <<
    endl;}

    private:
    vector<int> a;
    vector<int> b(10); // Syntax error!!!
    };

    int main()
    {
    foo y;

    return 0;
    }
    Dave Theese, Sep 16, 2003
    #1
    1. Advertising

  2. Dave Theese

    Ron Natalie Guest

    "Dave Theese" <> wrote in message news:...
    > In the code below, why does a syntax error appear at the location commented
    > "Syntax error!!!"? What language rule is coming into play here?
    >


    You can not intialize non-static members in their declaration in the class.
    That has to be done with the constructor. You obviously have the
    clue. The member "a" is done the right way.

    > class foo
    > {
    > public:
    > foo(): a(10) {for (int i = 0; i != a.size(); ++i) cout << a <<
    > endl;}
    >
    > private:
    > vector<int> a;
    > vector<int> b(10); // Syntax error!!!
    > };
    >
    Ron Natalie, Sep 16, 2003
    #2
    1. Advertising

  3. Dave Theese

    White Wolf Guest

    Dave Theese wrote:
    > Hello all,
    >
    > In the code below, why does a syntax error appear at the location
    > commented "Syntax error!!!"? What language rule is coming into play
    > here?
    >
    > The example below also shows an alternative (use of an initializer
    > list) to the illegal syntax I attempted to use, but I'm still left
    > wondering why I could not do what I attempted...
    >
    > Thanks,
    > Dave
    >
    > #include <iostream>
    > #include <vector>
    >
    > using namespace std;
    >
    > class foo
    > {
    > public:
    > foo(): a(10) {for (int i = 0; i != a.size(); ++i) cout << a
    > << endl;}
    >
    > private:
    > vector<int> a;
    > vector<int> b(10); // Syntax error!!!


    These are member variable _declarations_, not definitions. If you give an
    initializer it becomes a definition: the compiler understand it as "I want
    to create variable here". But there is no here, you are in the middle of a
    class definition.

    I am sure the standard has some nice words about it, much more complicated
    ones then I wrote. it may not even be exactly as I have said: but this is
    the basic idea. Class member variables have to be initialized by a
    constructor of that class.

    This can be frustrating, if you have 100 constructors and you have to write
    this b(10) into each of them. AFAIK there is a proposal (forming?) to add a
    possibility to "chain constructors", so that you only need to write such
    things once.

    --
    WW aka Attila
    White Wolf, Sep 16, 2003
    #3
  4. Dave Theese wrote:
    > Hello all,
    >
    > In the code below, why does a syntax error appear at the location commented
    > "Syntax error!!!"? What language rule is coming into play here?


    members need to be initialized in the constructor.


    One thing you could use is a helper template class - it will only work
    for parameters that can be made template parameters. Here is an example
    for a simple one parameter version of the template.

    BTW - any critique is welcome on this template. I'm about to "finish it
    up" and release the puppy in my GPL'd C++ library.


    template < typename w_type, typename w_init_type, w_init_type w_val >
    class AT_IType
    {
    public:

    w_type m_value;

    inline AT_IType()
    : m_value( w_val )
    {
    }

    inline operator w_type & ()
    {
    return m_value;
    }

    inline operator const w_type & () const
    {
    return m_value;
    }

    inline w_type & operator = ( const w_type & i_value )
    {
    m_value = i_value;

    return m_value;
    }

    };

    #include <vector>

    class Foo
    {
    public:
    AT_IType< std::vector<int>, int, 10 > x;

    };
    Gianni Mariani, Sep 16, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JFCM
    Replies:
    4
    Views:
    5,734
  2. CoolPint
    Replies:
    8
    Views:
    976
    Jeff Schwab
    Dec 14, 2003
  3. iluvatar
    Replies:
    8
    Views:
    357
    iluvatar
    Sep 13, 2006
  4. Lionel B
    Replies:
    5
    Views:
    360
    James Kanze
    Jul 19, 2007
  5. Shriramana Sharma
    Replies:
    6
    Views:
    252
    Shriramana Sharma
    Apr 5, 2013
Loading...

Share This Page