Hello all,
This may seem like a silly question but I suddenly wondered about this. Is
there any difference between the following 2 situations?
1)
class A
{
private SomeObject o = new SomeObject();
public A()
{}
}
2)
class A
{
private SomeObject o;
public A()
{
this.o = new SomeObject();
}
}
Is there any difference at all? If there is, what is it, maybe memory
allocation wise, or something, what method is 'better'? Or would it only
matter (obviously) if you have multiple constructors? Or am I questioning
myself the most weird and useless things ;)
Thanks! 7 2505
Assuming that you always want to create an instance of SomeObject,
Method
one is an example of defensive programming. It minimizes the chance of
forgetting to "initialize a variable" in the constructor or Init method. http://www.geocities.com/jeff_louie/OOP/oop4.htm
Regards,
Jeff 1)
class A
{
private SomeObject o = new SomeObject();
public A()
{}
}<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
thnx, all :)
"Razzie" <ra****@quicknet.nl> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl... Hello all,
This may seem like a silly question but I suddenly wondered about this. Is there any difference between the following 2 situations?
1)
class A { private SomeObject o = new SomeObject();
public A() {} }
2)
class A { private SomeObject o;
public A() { this.o = new SomeObject(); } }
Is there any difference at all? If there is, what is it, maybe memory allocation wise, or something, what method is 'better'? Or would it only matter (obviously) if you have multiple constructors? Or am I questioning myself the most weird and useless things ;)
Thanks!
I think the second is better.
Because Csharp is a OOP language .
This is a feeling , please follow the feeling .
"Razzie" wrote: Hello all,
This may seem like a silly question but I suddenly wondered about this. Is there any difference between the following 2 situations?
1)
class A { private SomeObject o = new SomeObject();
public A() {} }
2)
class A { private SomeObject o;
public A() { this.o = new SomeObject(); } }
Is there any difference at all? If there is, what is it, maybe memory allocation wise, or something, what method is 'better'? Or would it only matter (obviously) if you have multiple constructors? Or am I questioning myself the most weird and useless things ;)
Thanks!
ttl_web <tt*****@discussions.microsoft.com> wrote: I think the second is better. Because Csharp is a OOP language .
What does that have to do with it? I don't see why either approach is
more OO than the other.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
There is a rather subtle difference due to the order at which the
assignments happen, which is:
target class field initialization -> base class field initialization -> base
class constructor -> target class constructor
If the base class or the target class is finalizable, you may want to
consider the state of fields at finalization if an exception occurs before
the object is completely constructed. If you pre-initialize the fields,
they will have assigned values at finalization even if an error occurs in
either of the constructors. This may cause some unexpected problems at
finalization, including potential security issues. See http://blogs.msdn.com/cbrumme/archiv.../20/77460.aspx for details.
"Razzie" <ra****@quicknet.nl> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl... Hello all,
This may seem like a silly question but I suddenly wondered about this. Is there any difference between the following 2 situations?
1)
class A { private SomeObject o = new SomeObject();
public A() {} }
2)
class A { private SomeObject o;
public A() { this.o = new SomeObject(); } }
Is there any difference at all? If there is, what is it, maybe memory allocation wise, or something, what method is 'better'? Or would it only matter (obviously) if you have multiple constructors? Or am I questioning myself the most weird and useless things ;)
Thanks!
That is a very interesting reply and read! Thanks!
"Nicole Calinoiu" <ni*****@somewhere.net> wrote in message
news:er**************@TK2MSFTNGP11.phx.gbl... There is a rather subtle difference due to the order at which the assignments happen, which is:
target class field initialization -> base class field initialization ->
base class constructor -> target class constructor
If the base class or the target class is finalizable, you may want to consider the state of fields at finalization if an exception occurs before the object is completely constructed. If you pre-initialize the fields, they will have assigned values at finalization even if an error occurs in either of the constructors. This may cause some unexpected problems at finalization, including potential security issues. See http://blogs.msdn.com/cbrumme/archiv.../20/77460.aspx for details.
"Razzie" <ra****@quicknet.nl> wrote in message news:%2***************@TK2MSFTNGP11.phx.gbl... Hello all,
This may seem like a silly question but I suddenly wondered about this.
Is there any difference between the following 2 situations?
1)
class A { private SomeObject o = new SomeObject();
public A() {} }
2)
class A { private SomeObject o;
public A() { this.o = new SomeObject(); } }
Is there any difference at all? If there is, what is it, maybe memory allocation wise, or something, what method is 'better'? Or would it only matter (obviously) if you have multiple constructors? Or am I
questioning myself the most weird and useless things ;)
Thanks!
"ttl_web" <tt*****@discussions.microsoft.com> wrote in message
news:64**********************************@microsof t.com... I think the second is better.
why would you favor :
this.o = new SomeObject();
Using the this is redundant becahse the ctor is the default on (no name
clashing with parameters) and there are no local variables (yet) in the ctor
method, so no name clashing with parameters here either. It seems to be
just a bit noisy, and instead of making the code cleaner, more readable - it
seems to be a bit obfuscating - eschew obfuscation !
Because Csharp is a OOP language .
No it is not -- you must be thinking of smalltalk
regards
roy fine
This is a feeling , please follow the feeling .
"Razzie" wrote:
Hello all,
This may seem like a silly question but I suddenly wondered about this.
Is there any difference between the following 2 situations?
1)
class A { private SomeObject o = new SomeObject();
public A() {} }
2)
class A { private SomeObject o;
public A() { this.o = new SomeObject(); } }
Is there any difference at all? If there is, what is it, maybe memory allocation wise, or something, what method is 'better'? Or would it only matter (obviously) if you have multiple constructors? Or am I
questioning myself the most weird and useless things ;)
Thanks! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dan Perl |
last post by:
There is something with initializing mutable class attributes that I am
struggling with. I'll use an example to explain:
class Father:
attr1=None # this is OK
attr2= # this is wrong...
|
by: jjleto |
last post by:
When I run this simple program:
#include <vector>
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "" << endl; }
~A() { cout << "" << endl; }
|
by: amparikh |
last post by:
Ok, my question is not about Virtual destructors and why, but more on
the significance.
Generally we have a virtual destructor in the base class ( and
inadvertently in the derived class) so that...
|
by: qwerty2_reverse_iterator |
last post by:
Is this a bug with the ms compiler (V7.1)? (It seems so at least.)
I get errors when I don't initialize all the const pointer fields of an
anonymous union in a struct. Example:
//T2.h...
|
by: alacrite |
last post by:
If I have this situation
class X
{
Z z;
Y y;
};
Class X has two objects of type Z and Y. How do I initialize z and y
with non default constructors?
|
by: Gary Wessle |
last post by:
Hi
I am not sure how to initialize this.
B::B(int, double): /*...*/ {}
class A(
B b;
public:
A();
|
by: subramanian |
last post by:
Consider the following program:
#include <iostream>
#include <string>
class Test {
public:
Test(const std::string &val);
private:
|
by: JoeC |
last post by:
I have read books and have ideas on how to create objects. I often
create my own projects and programs. They end up getting pretty
complex and long. I often use objects in my programs they are...
|
by: Grey Alien |
last post by:
class A
{
public:
A(const B& ref);
private:
static B& b ;
};
How may b be initialized ?
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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...
|
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,...
| |