Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 7th, 2006, 01:35 AM
heng
Guest
 
Posts: n/a
Default How to initialize array which is a member of a class?

If the data member of a class is an array, how to initialize?

I tried the following, but it is wrong.

class A
{
public:
int a[3];
A():a({0,0,0}){}
};

Thanks for your kind help !

  #2  
Old December 7th, 2006, 02:25 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: How to initialize array which is a member of a class?

* heng:
Quote:
If the data member of a class is an array, how to initialize?
>
I tried the following, but it is wrong.
>
class A
{
public:
int a[3];
A():a({0,0,0}){}
};
If you just want to default-initialize (all zeroes for the above),

A(): a() {}

However, old versions of Visual C++ don't support that.

A better way is to use a std::vector,

struct A
{
std::vector<inta;
A(): a(3) {}
};

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  #3  
Old December 7th, 2006, 05:35 AM
Salt_Peter
Guest
 
Posts: n/a
Default Re: How to initialize array which is a member of a class?


heng wrote:
Quote:
If the data member of a class is an array, how to initialize?
>
I tried the following, but it is wrong.
>
class A
{
public:
int a[3];
A():a({0,0,0}){}
};
>
Thanks for your kind help !
Use a std::vector instead. Better yet, use a templated class with a
std::vector member.
That way you can store anything and you'll learn something about the
vector's interface.

#include <iostream>
#include <ostream>
#include <vector>
#include <list>
#include <string>
#include <iterator>

template< typename T >
class V {
std::vector< T vt; // private member vector
public:
V(size_t sz = 0, const T& t = T())
: vt(sz, t) { }
V(const V& copy)
{
vt = copy.vt;
}
/* size_type */
typedef typename std::vector< T >::size_type
size_type;
/* member functions */
void push_back(const T& t)
{
vt.push_back( t );
}
T& at(size_type idx)
{
return vt.at(idx);
}
T& operator[] (size_type idx)
{
return vt[idx];
}
size_type size() const { return vt.size(); }
/* iteration */
typedef typename std::vector< T >::const_iterator
const_iterator;
typedef typename std::vector< T >::iterator
iterator;
iterator begin() { return vt.begin(); }
const_iterator begin() const { return vt.begin(); }
iterator end() { return vt.end(); }
const_iterator end() const { return vt.end(); }
/* friend op */
friend std::ostream&
operator<<(std::ostream& os, const V& r_v)
{
std::copy( r_v.begin(),
--r_v.end(),
std::ostream_iterator< T >(os, ", ") );
return os << *(--r_v.end());
}
};

int main()
{
V< std::string vs;
vs.push_back( "string 0" );
vs.push_back( "string 1" );
vs.push_back( "string 2" );
std::cout << "vs.size() = ";
std::cout << vs.size() << std::endl;
std::cout << vs << std::endl;

V< double v(5, 1.1);
std::cout << "v.size() = ";
std::cout << v.size() << std::endl;
std::cout << v << std::endl;

V< double another(v);
another.push_back( 2.2 );
another.push_back( 3.3 );
std::cout << "another.size() = ";
std::cout << another.size() << std::endl;
std::cout << another << std::endl;

std::list< double dlist(another.begin(), another.end());
std::cout << "dlist.size() = ";
std::cout << dlist.size() << std::endl;
std::copy( dlist.begin(),
dlist.end(),
std::ostream_iterator< double >(std::cout, "\n") );
}

/*
vs.size() = 3
string 0, string 1, string 2
v.size() = 5
1.1, 1.1, 1.1, 1.1, 1.1
another.size() = 7
1.1, 1.1, 1.1, 1.1, 1.1, 2.2, 3.3
dlist.size() = 7
1.1
1.1
1.1
1.1
1.1
2.2
3.3
*/

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles