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

Objects initialization

I have a question about the following code in C#:

AddClass1 op1=new AddClass1();
op1.val=5;
AddClass1 op2=new AddClass1();
op2.val=3;
AddClass1 op3=op1+op2;
Console.WriteLine(op3.val);

The answer is of course 8. but my question is:
Why don't i need to initialize op3 in the form of AddClass1 op3=new
AddClass1();
like with op1 and op2 ?

Nov 17 '05 #1
1 1062
Hi Zorro26,
basically the + operator has been overridden in the AddClass1 type to
allow addition of two classes to produce a third result. So the + operator
knows how to handle the semantics of adding two AddClass1's together to
produce a third AddClass1 with the sumation result.

For example:

class AddClass1
{
private int m_intValue;

public AddClass1()
{
}

public int Value
{
get
{
return m_intValue;
}
set
{
m_intValue = value;
}
}

//Overload the + operator, so you can see how the binary + operator
//takes ac1,ac2 parameters and adds them together internally and returns
//a new instance with the result of the summation
public static AddClass1 operator + (AddClass1 ac1, AddClass1 ac2)
{
AddClass1 acResult;

acResult = new AddClass1();
acResult.Value = ac1.Value + ac2.Value;

return acResult;
}
}
That is why you can now say:
AddClass1 acResult = a1 + a2; //where a1,a2 are previously defined
instances of AddClass1 like in your example.
Hope that helps
Mark R Dawson
"Zorro26" wrote:
I have a question about the following code in C#:

AddClass1 op1=new AddClass1();
op1.val=5;
AddClass1 op2=new AddClass1();
op2.val=3;
AddClass1 op3=op1+op2;
Console.WriteLine(op3.val);

The answer is of course 8. but my question is:
Why don't i need to initialize op3 in the form of AddClass1 op3=new
AddClass1();
like with op1 and op2 ?

Nov 17 '05 #2

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

Similar topics

26
by: Uwe Mayer | last post by:
Hi, I've been looking into ways of creating singleton objects. With Python2.3 I usually used a module-level variable and a factory function to implement singleton objects. With Python2.4 I...
15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
4
by: junger | last post by:
If a member object is static, is it possible to initialize it with function calls? E.g., if I want my class to contain a static vector filled with other static objects, can I fill this vector...
15
by: Jakob Bieling | last post by:
Hi, I am aware of the fact, that the order of construction of global objects is unspecified. But I am in a situation, where I need to guarantee, that one object is created before others (not all...
3
by: Ann Huxtable | last post by:
Hi, I am writing a nested table structure to mimic data returned from a database (actually, a heirarchical recordset). I am representing the cells where the actual data is stored, by a union:...
1
by: benben | last post by:
Hi all! Does the standard say anything about the order of initialization of static class objects? For example: class A{}; class B { static A a; };
7
by: b83503104 | last post by:
Previously, when my constructor had no arguments, I used this to declare my objects: MyClass myObject; Now, my constructor has arguments (MyClass::MyClass(int someVariable)), how do I declare...
3
by: sk.rasheedfarhan | last post by:
Hi , Here I am new user to C#, my problem is I have to use dynamic Array of objects. But I heard C# don't support ptrs (using managed code C# support). In short i initialized objects of 1000...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
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
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
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...
0
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...

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.