根据我的理解,C++中的声明/初始化是具有"基本类型"的语句,后跟逗号分隔的声明符列表.
请考虑以下声明:
int i = 0, *const p = &i; // Legal, the so-called base type is 'int'.
// i is an int while p is a const pointer to an int.
int j = 0, const c = 2; // Error: C++ requires a type specifier for all declarations.
// Intention was to declare j as an int and c an as const int.
int *const p1 = nullptr, i1 = 0; // p1 is a const pointer to …Run Code Online (Sandbox Code Playgroud) 是否可以在不实例化的情况下在c ++中声明变量?我想做这样的事情:
Animal a;
if( happyDay() )
a( "puppies" ); //constructor call
else
a( "toads" );
Run Code Online (Sandbox Code Playgroud)
基本上,我只是想声明条件的外部,以便它获得正确的范围.
有没有办法在不使用指针和a在堆上分配的情况下执行此操作?也许引用聪明的东西?
下面的代码片段编译(演示):
struct A{ int i = 10; };
int main() {
struct A{ int i = 20; };
struct A;
struct A a;
}
Run Code Online (Sandbox Code Playgroud)
但这不是:
struct A{ int i = 10; };
int main() {
// struct A{ int i = 20; };
struct A;
struct A a;
}
Run Code Online (Sandbox Code Playgroud)
我可以看到答案可能是标准中的这些段落:
[basic.lookup.elab]/2和[basic.scope.pdecl]/7.
但我真的不知道如何从这两段中推断出上面显示的不同行为.
需要注意的是在第一个例子中,struct A是不是第一次在声明阐述类型说明符 struct A;,但在定义struct A中main().
在第二个例子中,struct A也未先在声明阐述型说明符 struct …
鉴于以下内容:
// not a problem
int i = 2, j = 3;
Run Code Online (Sandbox Code Playgroud)
这让我感到惊讶:
// compiler error: Implicitly-typed local variables cannot have multiple declarators
var i = 2, j = 3;
Run Code Online (Sandbox Code Playgroud)
不编译.也许有一些我不明白的事情(这就是我问这个的原因)?
但为什么编译器不会意识到我的意思:
var i = 2;
var j = 3;
Run Code Online (Sandbox Code Playgroud)
哪个会编译.
什么是未声明的标识符错误?什么是常见原因以及如何解决它们?
示例错误文本:
error C2065: 'cout' : undeclared identifier'cout' undeclared (first use in this function)我在for循环中使用指针时遇到问题。在for循环初始化程序中,我取消引用int指针并将其值设置为“ 0”。当我在循环中使用该取消引用的指针时,出现了段错误,并且我不明白为什么。我正在使用Code :: Blocks和C GNU GCC编译器。
看着观察窗口,我可以看到在for循环过程中变量有一个随机数。
似乎已取消引用的指针在for循环期间丢失了作用域。
代码:
#include <stdio.h>
int main(void)
{
int val = 0;
int *p = NULL;
int answer = 0;
p = &val;
*p = 1; // This dereferences and sets to one successfully
for (int i=3, (*p)=0 ; i>=0; i--) // Here *p is a random number
{
printf("do stuff");
(*p) += 1; // Here it causes a segmentation fault
} …Run Code Online (Sandbox Code Playgroud) 对于读取复杂指针声明,有左右规则.
但是这条规则没有提到如何阅读const修饰语.
例如,在简单的指针声明中,const可以通过多种方式应用:
char *buffer; // non-const pointer to non-const memory
const char *buffer; // non-const pointer to const memory
char const *buffer; // equivalent to previous declartion
char * const buffer = {0}; // const pointer to non-const memory
char * buffer const = {0}; // error
const char * const buffer = {0}; // const pointer to const memory
Run Code Online (Sandbox Code Playgroud)
现在用const指针声明指针怎么样?
char **x; // no const;
const char **x;
char * …Run Code Online (Sandbox Code Playgroud) 标准究竟如何定义,例如,float (*(*(&e)[10])())[5]声明一个类型的变量"引用指向函数的10指针的数组()返回指向5 float" 数组的指针?
我习惯于声明像这样的可变函数:
int f(int n, ...);
Run Code Online (Sandbox Code Playgroud)
阅读C++编程语言时,我发现书中的声明省略了逗号:
int f(int n...); // the comma has been omitted
Run Code Online (Sandbox Code Playgroud)
看起来这个语法是特定于C++的,因为当我尝试使用C编译器编译时出现此错误:
test.c:1:12: error: expected ‘;’, ‘,’ or ‘)’ before ‘...’ token int f(int n...);
写作int f(int n, ...)和int f(int n...)之间有什么区别吗?
为什么这个语法添加了C++?
在本教程中有写:
If you redeclare a JavaScript variable, it will not lose its value.
我为什么要重新声明变量?在某些情况下它是否实用?
谢谢
declaration ×10
c++ ×7
c ×2
const ×2
pointers ×2
c# ×1
c++11 ×1
definition ×1
for-loop ×1
function ×1
javascript ×1
name-lookup ×1
scope ×1
standards ×1
types ×1
variables ×1