473,663 Members | 2,800 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 1194
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*******@cana da.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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******** ************@co mcast.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
2156
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 "Some code here" class class2: propA = "A" def methB(self):
10
8163
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 effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That said, is there any real reason why I can't derive an otherwise empty
0
1356
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 templates: ------------------------------------------ in file "Model.h":
0
977
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 problem for C# as there is no concept of include. I faced problems for seperating declarations and definitions for class when it contains properties. This can also be done in context of struct/structures. Here is how we can. Class A
0
864
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 common to all pages, i did put some server hidden fields controls in the base class. Ex: (in the base class)
1
1359
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 all the common startup code for the pages on our website. It is derived from System.Web.UI.Page and every page in 2 separate projects (web applications) derives from it. It contains several declarations of server controls such a navigation bar...
5
2032
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 of a name declared in a class consists not only of the declarative region following the name's declarator, but also of all function bodies, default arguments, and constructor ctor-initializers in that class (including such things in nested classes).
1
2313
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. X<Zx(y) is fine. Tested with gcc 2.95, 3.3, 4.1, all gave the same error: t.cpp: In function 'int main()': t.cpp:44: error: no matching function for call to 'D<int>::D(D<int>&)'
6
2321
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 ClassB. But I get this Error from the compiler: and it's in ClassB... Error from the compiler: error C2061: syntax error : identifier 'TestA' error C2143: syntax error : missing ';' before '*'
15
7854
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
8858
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, 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...
0
8771
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8548
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8634
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 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...
0
7371
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, 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...
0
5657
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
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
2
1757
bsmnconsultancy
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...

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.