473,508 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class declarations

Is this legit? I thought that class members could not be allocated in the
class declaration. I thought this had to go into the constructor. If this
is OK, what are the associated rules?

class WhatEver
{
private object[] thePair=new object[2];

[STAThread]
static void Main(string[] args)
{

Nov 16 '05 #1
3 1186
Yes, it's legitimate. It's equivalent to putting the initialization in
the constructor. I believe that each initialization expression must be
stand-alone... that is, it can't use "this" to access other,
already-initialized members because the initialization order is not
guaranteed.

That said, I prefer to put everything in the constructor so that it's
all in one place. I find even class declarations like

int sales = 0;

to be annoying, because then I have to look two places (the constructor
and the member declaration) to see what the initial values are.

I know other people who avoid putting initialization code in
constructors and prefer member initializations. It's a style thing.

Nov 16 '05 #2
I concur. I always use the constructor. I was suprised to see this and it
made me wonder. Thanx

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Yes, it's legitimate. It's equivalent to putting the initialization in
the constructor. I believe that each initialization expression must be
stand-alone... that is, it can't use "this" to access other,
already-initialized members because the initialization order is not
guaranteed.

That said, I prefer to put everything in the constructor so that it's
all in one place. I find even class declarations like

int sales = 0;

to be annoying, because then I have to look two places (the constructor
and the member declaration) to see what the initial values are.

I know other people who avoid putting initialization code in
constructors and prefer member initializations. It's a style thing.

Nov 16 '05 #3
"web1110" <we***@comcast.net> wrote in message
news:Hd********************@comcast.com...
I concur. I always use the constructor. I was suprised to see this and it
made me wonder. Thanx


I don't concur.

I use both direct assignment and constructors.

The great thing with direct assignment is that you tell a future maintainer
that
- No, this member is not null.

Regards
- Michael S
Nov 16 '05 #4

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

Similar topics

9
2148
by: OKB (not okblacke) | last post by:
For a variety of reasons, I'm interested in putting together some code that will allow me to created structures out of nested classes, something like: class class1: def methA(self): print...
10
8147
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
0
1344
by: Leslaw Bieniasz | last post by:
Cracow, 16.09.2004 Hi, I have a problem with compiling the following construction involving cross-calls of class template methods, with additional inheritance. I want to have three class...
0
972
by: AnkitAsDeveloper [Ankit] | last post by:
As all we know, in order to remove cyclic includes in C++ we seperate the declarations and definitions of classs and it's member in two files Header (*.h) and source files (*.cpp). This is not a...
0
860
by: Rans | last post by:
Hello, I made a base class for all the pages in my project, so that instead of "public class CSomePage : System.Web.UI.Page" i got "public class CSomePage : CPage Since some parameters are...
1
1346
by: Chris | last post by:
This may be more of a Visual Studio question but those groups seem to be full of unrelated stuff so hopefully this might be the right place. I have a class (no associated aspx file) which handles...
5
2022
by: Steven T. Hatton | last post by:
If find the following excerpt from the Standard a bit confusing: <quote> 3.3.6 - Class scope -1- The following rules describe the scope of names declared in classes. 1) The potential scope...
1
2295
by: petschy | last post by:
hello, i've run into an error when qualifying a copy ctor 'explicit'. the strange thing is that i get a compiler error only if the class is a template and declare the variable as X<Zx = y....
6
2312
by: babakandme | last post by:
Hi to every body...:D I'm a novice C++ programmer & I've a question, I have the ClassA & in it's constructor, I instantiate ClassB, and I want send "this" pointer """pointer to ClassA""" to the...
15
7839
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
7226
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
7125
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
7328
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
7499
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
5631
agi2029
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,...
0
3199
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
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
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 ...
1
767
muto222
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.