namespace怎么用,C关于类中定义静态变量的使用

1 , C关于类中定义静态变量的使用缺少初始化 。静态变量必须初始化才能使用!#include using namespace std;class Apublic: static int a;};int A::a = 0;void main()A::a=10;cout<}static用法有很多 。1.如果修饰全局变量,那么该全局静态变量仅能在本文件内进行访问 。属于限定作用域的用途 。2.如果修饰类的成员变量,那么该静态成员变量就会放到静态区进行存储,可以通过类名::的方式进行访问 。3.如果修饰类的成员函数 , 那么该静态成员函数可以通过类型::的方式进行访问 。4.如果修饰函数内的局部变量,那么该静态局部变量在类执行完毕后能够继续保存 。下次执行该函数时,可以利用上次的结果 。#include using namespace std;using std::endl;//endl需要定义class Apublic: int a;//不能定义成静态成员,否则只能在类中访问};void main() A Ca;//成员只能够用最想访问 , 类名只是一种数据类型 Ca.a=10;//a是成员变量,不是成员函数,不能用作用域操作符 cout<}
2,freopen函数的用法输入输出重定向例:文件名为:r.txt输入重定向到文件r.txt:freopen("r.txt","r",stdin);输出到文件:freopen("r.txt","w",stdout);参数说明:(文件名,方式(r为读?。?w为写入),照打就ok了);若文件名出写:"CON",为重定向回控制台但这个函数好像和system的函数有点不合 , 具体在用完重定向输出回控制台后,再用system的函数后再试试输出 你就懂了....freopen是被包含于C标准库头文件<stdio.h>中的一个函数,用于重定向输入输出流 。该函数可以在不改变代码原貌的情况下改变输入输出环境,但使用时应当保证流是可靠的.头文件:stdio.hC89函数声明:FILE *freopen( const char *filename, const char *mode, FILE *stream );[1] C99函数声明:FILE *freopen(const char * restrict filename, const char * restrict mode, FILE * restrict stream);形参说明:filename:需要重定向到的文件名或文件路径 。mode:代表文件访问权限的字符串 。例如,"r"表示“只读访问”、"w"表示“只写访问”、"a"表示“追加写入” 。stream:需要被重定向的文件流 。返回值:如果成功,则返回该指向该输出流的文件指针,否则返回为NULL 。freopen函数没用错,下面是百度百科上的示例程序: #include using namespace std; int main() { freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); int a,b; while(scanf("%d%d",&a,&b)!=eof) printf("%d\n",a+b); fclose(stdin); fclose(stdout); return 0; }
3,转载在C中如何使用自定义的命名空间在C#中如何使用自定义的命名空间?一、建立网站解决方案:Data.cs文件中的源码如下所示:using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;namespace Alex.Data{ /// /// Summary description for Data /// public class HelloData { public HelloData() { // // TODO: Add constructor logic here // } public string GetHello() { return "Hello ASP.NET!"; } }}Dal.cs文件中的源码如下所示:using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;namespace Alex.Dal{ /// /// Summary description for Dal /// public class HelloDal { private readonly Alex.Data.HelloData hd = new Alex.Data.HelloData(); public HelloDal() { // // TODO: Add constructor logic here // } public string GetHello() { return hd.GetHello(); } }}二、编译:接下来就可以编译了 , 在控制台中输入命令"csc /target:library Data.cs",可以看到编译成功了 , Data文件夹下多了个Data.dll链接库文件; 接着使用同样的方法编译Dal.cs,不同的是在Dal.cs中引用了Data.cs中的命名空间Alex.Data,所以编译命令稍微有点不同,添加了一个/r参数;编译命令为"csc /target:library /r:../Data/Data.dll Dal.cs" 。有一点需要注意的是Data.cs和Dal.cs的编译顺序不能变,必须先编译Data.cs再编译Dal.cs;因为在编译Dal.cs的时候需要引用Data.cs中的命名空间,所以必须先编译Data.cs再编译Dal.cs 。三、添加引用:最后一步就是添加引用了 。在解决方案上右键点击,选择"Add Reference...",然后选择“Browser”选项卡,定位到刚才编译生成的DLL文件位置,选中,确定 。重复两次 , 将两个DLL文件都添加到引用中;四、测试 所有的工作做完了,接下来就要测试一下能否引用刚刚自定义的命名空间 。在Default.aspx.cs中添加如下代码:using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using Alex.Dal;public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { HelloDal hd = new HelloDal(); Response.Write(hd.GetHello()); }}按下Ctrl+F5,在浏览器中看到打印出了"Hello ASP.NET!",说明引用成功,如下如所示:补充: 在实际开发时,若每次修改源文件都去重新编译 , 添加引用,似乎麻烦了点 , 因此可以写一个批处理文件,让它自动编译生成DLL文件,并添加到引用中,下面是批处理文件,可以参考 。【namespace怎么用,C关于类中定义静态变量的使用】

    推荐阅读