473,320 Members | 2,117 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,320 software developers and data experts.

Multiple variable inialization

Hi there.

I was wondering why the following two constructions dont work as expected:

1.) bool a, b, c, d = false;

2.) bool a, b, c, d;
a, b, c, d = false;

But the following works.
bool a, b, c, d;
a = b = c = d = false;

TIA


Jul 22 '05 #1
3 1274
Swartz wrote:
Hi there.

I was wondering why the following two constructions dont work as expected:

1.) bool a, b, c, d = false;
This means this :

bool a;
bool b;
bool c;
bool d = false;

2.) bool a, b, c, d;
a, b, c, d = false;
This is using the "comma" operator.

It may be re-written as;

a; b; c; d=false; // well alomost

But the following works.
bool a, b, c, d;
a = b = c = d = false;


This means :

(a=(b=(c=(d=false))));
You can do somthing like:

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

};

AT_IType <bool,bool,false> a, b, c, d;
Jul 22 '05 #2
Swartz wrote:
Hi there.

I was wondering why the following two constructions dont work as
expected:


That depends on what you expect. They work as expected here.

Jul 22 '05 #3
On Mon, 26 Jan 2004 02:04:08 -0600, "Swartz" <sw****@inbox.ru> wrote:
Hi there.

I was wondering why the following two constructions dont work as expected:

1.) bool a, b, c, d = false;
bool a = false, b = false, c = false, d = false;

You have to initialize each one separately. Often the advice in C++ is
to have only 1 variable per declaration. e.g.

bool a = false;
bool b = false;
bool c = false;
bool d = false;

is clearer.

2.) bool a, b, c, d;
a, b, c, d = false;
The comma operator is used to separate expressions and return the
right hand expression. So that is exquivalent to:
evaluate a, discard the value
evaluate b, discard the value
evaluate c, discard the value
evaluate "d = false", discard the value.
So only d is modified.

Using the comma operator in an unusual way:
a = false, b = false, c = false, d = false;
or in conventional syntax:
a = false; b = false; c = false; d = false;
But the following works.
bool a, b, c, d;
a = b = c = d = false;


It should be obvious why that works, but for the others you just have
to understand declaration syntax and the comma operator.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #4

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

Similar topics

5
by: Charles L | last post by:
Can someone explain to me what the following means? "C permits multiple definitions of a variable in any given namespace, provided the definitions are the same and it generates only a single...
6
by: Ben Hallert | last post by:
Hi guys, I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable number of select-multiple inputs on it. ...
14
by: Carramba | last post by:
hi! I have program with several funktion witch are in separete files, I have one include file were I have definet some variables and initiated 'const double fVar=0.874532;' this files is includet...
9
by: lbj137 | last post by:
I have two files: A.c and B.c. In both files I define a global variable, int xxxx; When I compile with a green hills compiler (and also i think with a GNU compiler) I get no errors or warnings....
15
by: Iced Crow | last post by:
In C# I know that you can use delegates to assing multiple addresses of sub and functions to a delegate and have it fire multiple procedures... How do I do this in VB? I only know of assigning...
9
by: Bob Day | last post by:
VS 2003, vb.net , sql msde... I have an application with multiple threads running. Its a telephony application where each thread represents a telephone line. For code that would be the same...
11
by: lars.uffmann | last post by:
Easily described problem: Using g++ version 3.3.5 under suse 9.3, bla.h: ----------- #ifndef myTEST #define myTEST ZFSInt test; #endif
7
by: annerhexian | last post by:
hi to all i am confused right now.. im just new in php.. and i have syntax error in my codes.. pls take a look? database name: ranking table: form1 fields: form1academicbachelor ...
2
by: Immortal Nephi | last post by:
You may have heard diamond shape. You create one base class. One base class has member functions and member variables. You create two derived classes. All member functions and member variables...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.