Hi,
Do you know how can I intialize an array member:
for example
class A
{
public:
A(int x);
};
class B
{
public:
B();
private:
A a[2];
};
how do I intialize a?
Thanks
Avner 4 2293
Avner Flesch wrote: Hi,
Do you know how can I intialize an array member: for example class A { public: A(int x); };
class B { public: B(); private: A a[2]; };
how do I intialize a?
You can't. C++ doesn't offer a way to initialize array members in the
initializer list. You have to get the members default-constructed and
then set whatever you need in the constructor body.
"Avner Flesch" <av***@il.marve ll.com> wrote in message
news:3F******** ******@il.marve ll.com... Hi,
Do you know how can I intialize an array member: for example class A { public: A(int x); };
class B { public: B(); private: A a[2]; };
how do I intialize a?
Same way you initialize any other member(s), with a constructor.
B::B()
{
a[0] = A(1);
a[1] = A(2);
}
This isn't technically 'initialization ', it's assignment, but
its the best that can be done with an (non static) array member.
The language does not provide an initializer list syntax
for arrays. Maybe consider a container (or perhaps a std::pair)
instead.
-Mike
Mike Wahler wrote: "Avner Flesch" <av***@il.marve ll.com> wrote in message news:3F******** ******@il.marve ll.com... Hi,
Do you know how can I intialize an array member: for example class A { public: A(int x); };
class B { public: B(); private: A a[2]; };
how do I intialize a? Same way you initialize any other member(s), with a constructor.
The difference is that array members can only be default initialized.
B::B() { a[0] = A(1); a[1] = A(2); }
This isn't technically 'initialization ', it's assignment, but its the best that can be done with an (non static) array member.
"technicall y" or not. It's not initialization. The initialization
happend before the constructor of B was entered.
The language does not provide an initializer list syntax for arrays. Maybe consider a container (or perhaps a std::pair) instead.
I guess if the number was only 2 anyway, just two separate member
variables would be the easiest way.
I don't think there is a way to initialize an array with anything
other than a default constructor.
You could change the array to an array of pointers and then fill
in the array by new for individual objects at run-time.
Sandeep
-- http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Generate Sequence Diagrams and Use Case Diagrams in PDF This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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:
|
by: jut_bit_zx |
last post by:
class A
{
public:
A();
virtual ~A(){}
....
private:
int m_iarray;
}
|
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
private string myArray;
|
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 use my specific
constructor to initialize the class. How to do it without using malloc?
Something like Point* pt = new Point; I want it to reserve the
space for N points only, and not to call default constructor. I dont
have a default...
|
by: wenmang |
last post by:
hi,
I have following:
struct array1
{
int id;
char *name;
};
struct array2
{
| |
by: thinktwice |
last post by:
char a = { 0 }
is it ok?
|
by: heng |
last post by:
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;
A():a({0,0,0}){}
};
|
by: Ehud Shapira |
last post by:
Is it possible to have a declaration of a struct pointer initialized
to an unnamed struct?
(I'm only concerned with static/global variables, if it matters.)
I'm trying to do something like:
struct st_a {
int i, j;
};
|
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;
};
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |