473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 !

Dec 7 '06 #1
2 2058
* heng:
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?
Dec 7 '06 #2

heng wrote:
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
*/

Dec 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Avner Flesch | last post by:
Hi, Do you know how can I intialize an array member: for example class A { public: A(int x); };
13
by: Kyle | last post by:
Hi, Is it possible to initialize a constant memeber array in a class? I tried several syntax but all failed. class A { public: A(); ~A(); private:
5
by: Jim Langston | last post by:
What I want to do: have a vector of ints in my class initialized with 0 to 499 which will later be pushed/popped out of the vector by instances. What I have: class CParticleStream // Yes, I...
3
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
18
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and...
15
by: thinktwice | last post by:
char a = { 0 } is it ok?
8
by: aaragon | last post by:
Hi, just a very simple question. I was wondering what is the most efficient way of initializing an array. I have a very simple class that wraps an array to provide bound checking, something like...
11
by: Bob Altman | last post by:
Hi all, I have a class that contains a member variable that is an array of class instances: class MyClass { private: SomeClass m_someClass; SomeClass m_arrayOfClasses; };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.