关于 各语言的 readonly。

同事SW跟我说过 C# 和 C++ 中 readonly 的不同写法:

C#:

public class ConstTest
{
    public int Age;
    public ConstTest(int TheAge)
    {
        this.Age = TheAge;
    }
}

public class TheTest
{
    public static readonly ConstTest theTest = new ConstTest(5);
}

class CryptoMemoryStream
{
    public static void Main()
    {
        Console.WriteLine(TheTest.theTest.Age);
    }
}



C++:

#pragma once
#include "stdafx.h"

using namespace std;

class ConstTest
{
public:
 int Age  ;
 ConstTest::ConstTest(int TheAge) ;
};

ConstTest::ConstTest(int TheAge)
{
 Age = TheAge ;
}
class  TheTest
{
public :
  static const ConstTest theTest ;
  TheTest::TheTest() ;
};
const ConstTest TheTest::theTest  =   ConstTest(5) ;

int _tmain(int argc, _TCHAR* argv[])
{
 std::cout<< TheTest::theTest.Age ;
 return 0;
}



请SW跟贴。
posted @ 2007-08-19 09:43  NewSea  阅读(244)  评论(1编辑  收藏  举报