我想知道在静态类中,所有方法和数据成员应该是静态的还是可以找到非静态成员?
number = static_cast<int>(argv[1]);
Run Code Online (Sandbox Code Playgroud)
错误:使用static_cast从char*转换为int不允许.
我试过在谷歌上找出原因,我似乎无法找到它.另外,我不想得到ascii值,因为argv [1]是一个数字.
例如./prog 15
cout <<数字; //想要打印15
代码优先:
#include <stdio.h>
void foo()
{
static int bar;
}
int main()
{
bar++;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器(Clang)抱怨:
static.c:10:2: error: use of undeclared identifier 'bar'
Run Code Online (Sandbox Code Playgroud)
该语句static int bar;中是否应该foo()给出bar静态存储持续时间,这使得它在main函数之前声明并初始化?
下面的 static 关键字代码在调用函数之前和之后给出了两个不同的输出
#include<stdio.h>
static int count=5;
int fun()
{
count = 0;
count++;
return count;
}
int main()
{
printf("%d ", count);
printf("%d ", fun());
printf("%d ", fun());
printf("%d ", count);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:5 1 1 1
问)为什么 count 给出两个不同的值;一开始是5,调用函数后就变成1了?
我是一个java新手,尽管到处搜索,我找不到静态实际做什么的基本定义.有人可以告诉我这是什么意思吗?另外,请说出你的答案,好像我甚至不知道java是什么,没有编程语言的例子吗?非常感谢.编辑:所以我的理解是,当你在构造函数中有一个静态变量时,
i.e. you have
class test{
static int a = 5;
public test(){
}
}
Run Code Online (Sandbox Code Playgroud)
然后
test test1 = new test();
test test2 = new test():
Run Code Online (Sandbox Code Playgroud)
,test1.a等于5,test2.a也等于5.如果你改变了test1.a = 6,test2.a也会等于6?