标签: declaration

C++中的声明

根据我的理解,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++ pointers const declaration language-lawyer

48
推荐指数
2
解决办法
2020
查看次数

在用c ++初始化对象之前声明一个对象

是否可以在不实例化的情况下在c ++中声明变量?我想做这样的事情:

Animal a;
if( happyDay() ) 
    a( "puppies" ); //constructor call
else
    a( "toads" );
Run Code Online (Sandbox Code Playgroud)

基本上,我只是想声明条件的外部,以便它获得正确的范围.

有没有办法在不使用指针和a在堆上分配的情况下执行此操作?也许引用聪明的东西?

c++ scope declaration instantiation

47
推荐指数
7
解决办法
4万
查看次数

下面的第一个片段编译,但第二个片段不编译.为什么?

下面的代码片段编译(演示):

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 Amain().

在第二个例子中,struct A先在声明阐述型说明符 struct …

c++ declaration definition language-lawyer name-lookup

47
推荐指数
3
解决办法
4188
查看次数

为什么C#不允许使用var声明多个变量?

鉴于以下内容:

// 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)

哪个会编译.

c# declaration implicit-typing variable-declaration

46
推荐指数
3
解决办法
1万
查看次数

什么是"未声明的标识符"错误,如何解决?

什么是未声明的标识符错误?什么是常见原因以及如何解决它们?

示例错误文本:

  • 对于Visual Studio编译器: error C2065: 'cout' : undeclared identifier
  • 对于GCC编译器: 'cout' undeclared (first use in this function)

c++ compiler-errors declaration undeclared-identifier

46
推荐指数
4
解决办法
28万
查看次数

在“ for”循环初始化器中取消引用指针会产生分段错误

我在for循环中使用指针时遇到问题。在for循环初始化程序中,我取消引用int指针并将其值设置为“ 0”。当我在循环中使用该取消引用的指针时,出现了段错误,并且我不明白为什么。我正在使用Code :: Blocks和C GNU GCC编译器。

  1. 看着观察窗口,我可以看到在for循环过程中变量有一个随机数。

  2. 似乎已取消引用的指针在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)

c pointers for-loop declaration

45
推荐指数
2
解决办法
1496
查看次数

读复杂的const声明的简单规则?

对于读取复杂指针声明,有左右规则.

但是这条规则没有提到如何阅读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)

c c++ const declaration

42
推荐指数
2
解决办法
1万
查看次数

什么是声明和声明符以及它们的类型如何按标准解释?

标准究竟如何定义,例如,float (*(*(&e)[10])())[5]声明一个类型的变量"引用指向函数的10指针的数组()返回指向5 float" 数组的指针?

受到与@DanNissenbaum讨论的启发

c++ standards types declaration c++11

42
推荐指数
1
解决办法
6027
查看次数

在C++中的可变参数函数声明中省略了逗号

我习惯于声明像这样的可变函数:

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++?

c++ declaration function variadic-functions

42
推荐指数
4
解决办法
2051
查看次数

重新声明一个javascript变量

教程中有写:

If you redeclare a JavaScript variable, it will not lose its value.

我为什么要重新声明变量?在某些情况下它是否实用?

谢谢

javascript variables declaration

41
推荐指数
5
解决办法
2万
查看次数