473,473 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

constructor question

I write a class as below:

class c1 {

public:

c1(): a(2), c[4]('\0') {} //LINE1

private:

int a;
char c[5];
}

But LINE1 can not pass compiling.

I modify it as:
class c1 {

public:

c1(): a(2){
c[4] = '\0'; //LINE1
}

private:

int a;
char c[5];
}

It works. Is there a better way to write the constructor? Thanks.

Jun 13 '06 #1
3 1586
ju******@gmail.com wrote:
I write a class as below:

class c1 {

public:

c1(): a(2), c[4]('\0') {} //LINE1
If you want to default-initialise your array, use the () syntax:

c1(): a(2), c() {}

There is no other option available to you. By doing "c()" you
set all members to zero.

private:

int a;
char c[5];
}

But LINE1 can not pass compiling.

I modify it as:
class c1 {

public:

c1(): a(2){
c[4] = '\0'; //LINE1
}

private:

int a;
char c[5];
}

It works. Is there a better way to write the constructor? Thanks.


See above. Unfortunately you don't have many choices. AFAIK, the
Committee is working on some improvements.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 13 '06 #2
> >
But LINE1 can not pass compiling.

I modify it as:
class c1 {

public:

c1(): a(2){
c[4] = '\0'; //LINE1
}

private:

int a;
char c[5];
}


If I only want to initialize the last element of the array c as a NULL
terminator, is there a better way to do it?

Jun 13 '06 #3
ju******@gmail.com wrote:
>
> But LINE1 can not pass compiling.
>
> I modify it as:
> class c1 {
>
> public:
>
> c1(): a(2){
> c[4] = '\0'; //LINE1
> }
>
> private:
>
> int a;
> char c[5];
> }
>


If I only want to initialize the last element of the array c as a NULL
terminator, is there a better way to do it?


No.
Jun 13 '06 #4

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

Similar topics

10
by: cppaddict | last post by:
Hi, I am writing a program and needs to know one of its object members before it can be initialized. It doesn't really matter for my question (which a C++ question, not a windows question), but...
24
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
6
by: Fred Zwarts | last post by:
Hello, I am trying to debug some complex debug code. In order to track the use of dynamically allocated memory, I replaced the standard global new and delete operators. (Not for changing the...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
2
by: david | last post by:
Well, as a matter of fact I_HAD_MISSED a basic thing or two, anyway, although Ollie's answer makes perfectly sense when dealing with classes, it doesn't seem to me to apply as well if you have to...
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
19
by: Jeroen | last post by:
Hi guys, I have a simple question. If I have a class like: class A { A(); ~A(); A(A& a); A(int i);
1
by: Bob Johnson | last post by:
Please note that this question is NOT about the merits of Microsoft certification (no need to get into that here). While taking a MS certification exam, I came across a question where it was...
4
by: Rahul | last post by:
Hi Everyone, It is well known that the input parameter which is passed to the copy constructor is passed as reference and not as as object. Because passing an object is as good as making another...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.