地址 指针 及 引用 测试

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    string h1 = "hello";
    string h2 = h1;
    printf("address of h1:%X,Value=%s\r\n",h1.c_str(),h1.c_str());
    printf("address of h2:%X,Value=%s\r\n\r\n",h2.c_str(),h2.c_str());
    h1[2] = '2';
    //h2[2] = '3';
    printf("address of h1:%X,Value=%s\r\n",h1.c_str(),h1.c_str());
    printf("address of h2:%X,Value=%s\r\n\r\n",h2.c_str(),h2.c_str());
    h1[2] = 'X';
    h2[3] = 'Y';//<---COPY ON WRITE OCCUR
    printf("address of h1:%X,Value=%s\r\n",h1.c_str(),h1.c_str());
    printf("address of h2:%X,Value=%s\r\n",h2.c_str(),h2.c_str());    
    system("PAUSE");
    return EXIT_SUCCESS;
}

 

C#

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Text.StringBuilder h1 = new System.Text.StringBuilder("hello");
            System.Text.StringBuilder h2 = h1;
            string h3 = h1.ToString();
            string h4 = "hello";
            h1[2] = 'y';
            h2[2] = 'm';
            Console.WriteLine(h1.ToString());
            Console.WriteLine(h2.ToString());
            Console.WriteLine(object.Equals(h4, h3) ? "Yes" : "No");
            Console.WriteLine(object.ReferenceEquals(h4,h3) ? "Yes" : "No");

            int a = 5;
            double b = 5;
            Console.WriteLine(object.Equals(a, b) ? "Yes" : "No");
            Console.WriteLine((a==b) ? "Yes" : "No");
            Console.WriteLine(object.ReferenceEquals(a, b) ? "Yes" : "No");
            

            Console.Read();
        }
    }
}

posted on 2011-01-13 22:33  钢铁奏鸣曲  阅读(147)  评论(0编辑  收藏  举报

导航