C-value type and reference type

Source: Internet
Author: User

The non-reference parameter initializes the parameter by using the copy of the real parameter. Modifying the value of the parameter does not affect the value of the real parameter. However, copying the real parameter is not suitable in all situations, the following situations are not suitable for copying real parameters:

L when you need to modify the value of the real parameter in the function

L when a large object needs to be passed as a real parameter. For actual applications, the time and storage cost of copying objects are often too large.

L when there is no way to copy real parameters.

It is common for the first case, while the second case is to take into account the performance. For example, the following program uses the form parameter as the reference type, this is because I want to copy the ifstream content to the vector. However, ifstream reads a large file, which may be as large as 10 MB, of course, the final generated vector will also have a size of 10 MB. If the Traditional Copy parameter is used to pass the ifstream, then a temporary vector is used to save the content, and then a copy of the vector is returned, obviously, a lot of time and space are wasted.

Of course, this program does not want to modify the value of the real parameter ifstream. At this time, you can declare the form parameter as the const type. It can accept non-const real parameters.

// Function. Put the ifstream content in the vector. hasOtherValue indicates whether the content is in a row. Besides the original word, it also includes Word Frequency and part of speech.

Void ifsreamToVector (ifstream & fin, vector <string> & v, bool hasOtherValue = true ){
V. clear ();
String str; // store words

If (hasOtherValue ){
// In addition to the original word, the line also contains the word frequency and part of speech.
Int itmp; // Word Frequency
String stmp; // stores parts of speech
While (fin> str> itmp> stmp ){
V. push_back (str );
}
} Else {
While (fin> str ){
V. push_back (str );
}
}
}

These questions are not used in website creation, but some basic concepts...
The difference between the value type and the reference type is that when values are assigned and passed as parameters...
When a value is assigned, the value type is assigned a new variable (that is, copying), while the reference type only assigns the address to the new object.
When a parameter is passed, the value type is to pass the parameter value, while the reference type is to pass the address as the parameter.
The following example...
// Value Type assignment.

Public void valuttype ()
{
Int a = 10;
Int B =;
B = 20;
Console. WriteLine ("a = {0}, B = {1}", a, B );
}
The output result is a = 10 and B = 20.
// Class type assignment.
Public class Test
{
Int;
Public int
{
Get
{
Return;
}
Set
{
A = value;
}
}
}

Public void classtype ()
{
Test t = new Test ();
T. A = 10;
Test t1 = t;
T1.A = 20;
Console. WriteLine ("t. A = {0}, t1.A = {1}", t. A, t1.A );
}
Output result: t. A = 20, t1.A = 20;
// Parameter example.
Public void Test ()
{
Int a = 10;
Test t = new Test ();
T. A = 10;
Parameter (a, t );
Console. WriteLine ("Test: a = {0}, t. A = {1}", a, t. );
}

Public void Parameter (int a, Test t)
{
A = 20;
T. A = 20;
Console. WriteLine ("Parameter: a = {0}, t. A = {1}", a, t. );
}


The output result is:
Parameter: a = 20, t. A = 20
Test: A = 10, t. A = 20

The above is the difference between the value type and the reference type.

Interface, which best reflects the inheritance, encapsulation, and Polymorphism of classes.

Interface IFruit
{
Void MyName ();
}

Class Apple: IFruit
{
// Explicit interface member implementation:
Void IFruit. MyName ()
{
// Method implementation.
Console. WriteLine ("MyName is Apple ");
}
}

Class Orange: IFruit
{
// Explicit interface member implementation:
Void IFruit. MyName ()
{
// Method implementation.
Console. WriteLine ("MyName is Orange ");
}
}

Public class Test
{
Static void Main ()
{
// Declare an interface instance.
IFruit obj = new Apple ();

// Call the member.
Obj. MyName ();
Obj = new Orange ();
Obj. MyName ();
}
}

Output:
MyName is Apple
MyName is Orange

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.