先学习了一个例子,面向对象的
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
__gc class Animal
{
public:
int legs;
void SetName(String *name)
{ strName = strName->Copy(name); }
String* GetName() { return strName; }
private:
String *strName;
};
int _tmain()
{
// TODO: Please replace the sample code below with your own.
Animal *cat, *dog;
cat = new Animal;
dog = new Animal;
cat->SetName("Cat");
cat->legs = 4;
dog->SetName("Dog");
dog->legs = 4;
Console::WriteLine("Animal 1");
Console::Write("Name: ");
Console::WriteLine(cat->GetName());
Console::Write("Legs: ");
Console::WriteLine(cat->legs);
Console::WriteLine();
Console::WriteLine("Animal 2");
Console::Write("Name: ");
Console::WriteLine(dog->GetName());
Console::Write("Legs: ");
Console::WriteLine(dog->legs);
Console::WriteLine();
return 0;
}
很好的一个例子,用到了托管代码,__gc,要注意的是,调试时,用到了托管代码,所以要在
项目->属性->c/c++选项->常规->编译为托管 选择 程序集支持 (/clr)
就可以了,值得初学者注意
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
__gc class Animal
{
public:
int legs;
void SetName(String *name)
{ strName = strName->Copy(name); }
String* GetName() { return strName; }
private:
String *strName;
};
int _tmain()
{
// TODO: Please replace the sample code below with your own.
Animal *cat, *dog;
cat = new Animal;
dog = new Animal;
cat->SetName("Cat");
cat->legs = 4;
dog->SetName("Dog");
dog->legs = 4;
Console::WriteLine("Animal 1");
Console::Write("Name: ");
Console::WriteLine(cat->GetName());
Console::Write("Legs: ");
Console::WriteLine(cat->legs);
Console::WriteLine();
Console::WriteLine("Animal 2");
Console::Write("Name: ");
Console::WriteLine(dog->GetName());
Console::Write("Legs: ");
Console::WriteLine(dog->legs);
Console::WriteLine();
return 0;
}
很好的一个例子,用到了托管代码,__gc,要注意的是,调试时,用到了托管代码,所以要在
项目->属性->c/c++选项->常规->编译为托管 选择 程序集支持 (/clr)
就可以了,值得初学者注意