Initialization of data members

D

Dave Theese

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;
}
 
R

Ron Natalie

Dave Theese said:
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!!!
};
 
W

White Wolf

Dave said:
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.
 
G

Gianni Mariani

Dave said:
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;

};
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top