标签: declaration

C,C++,C#,Java和Python中的声明,定义,初始化

这些术语在上述每种语言中的含义是什么?在这方面,为什么语言不同(无论他们做什么,如果他们做的话)?

language-design initialization declaration definition

10
推荐指数
1
解决办法
5118
查看次数

在for循环中声明和初始化变量

我可以写简单吗?

for (int i = 0; ...
Run Code Online (Sandbox Code Playgroud)

代替

int i;
for (i = 0; ...
Run Code Online (Sandbox Code Playgroud)

在C或C++?

(并且变量i只能在循环内访问吗?)

c c++ for-loop declaration

10
推荐指数
3
解决办法
5384
查看次数

对于泛型抽象类,使用包的__init__.py模块是pythonic吗?

我正在根据python3中的面向对象模型为我的公司开发一个相当复杂的应用程序.该应用程序包含几个包和子包,每个包当然包含一个__init__.py模块.

我主要使用那些__init__.py模块为它们内部的包声明泛型类,它们仅用作各自包的抽象模板.

我现在的问题是:这是一个使用__init__.py模块的"好"/"正确"/"pythonic"方式吗?或者我宁愿在其他地方声明我的泛型类?

举个例子,让我们假设一个包mypkg:

mypkg.__init__.py:

class Foo(object):
    __some_attr = None

    def __init__(self, some_attr):
        self.__some_attr = some_attr

    @property
    def some_attr(self):
        return self.__some_attr

    @some_attr.setter
    def some_attr(self, val):
        self.__some_attr = val
Run Code Online (Sandbox Code Playgroud)

mypkg.myfoo.py:

from . import Foo

class MyFoo(Foo):
    def __init__(self):
        super().__init__("this is my implemented value")

    def printme(self):
        print(self.some_attr)
Run Code Online (Sandbox Code Playgroud)

python abstract-class declaration python-3.x

10
推荐指数
1
解决办法
3450
查看次数

如果我用一个空参数表声明一个函数,然后传递参数怎么办?

例如,

#include <stdio.h>

void foo();

int main(void)
{
        foo();
        foo(42);
        foo("a string", 'C', 1.0);
        return 0;
}

void foo()
{
        puts("foo() is called");
}
Run Code Online (Sandbox Code Playgroud)

输出:

foo() is called
foo() is called
foo() is called
Run Code Online (Sandbox Code Playgroud)

这段代码编译得很好(没有使用clang的警告)并运行良好.但我想知道传递给的值会发生什么foo()?他们被推入堆栈还是被丢弃?

也许这个问题听起来毫无用处,但它确实有意义.例如,当我有int main(),而不是int main(void)传递一些命令行参数时,行为main()会受到影响吗?

此外,在使用时<stdarg.h>,...ISO C 之前至少需要一个命名参数.我们是否可以使用这样的声明void foo()从函数的零参数传递到无限参数?

我注意到这void foo()是一个"非原型声明",这void foo(void)是一个"原型宣言".这有点相关吗?


澄清

似乎这个问题被标记为重复,空参数列表是什么意思?[重复](有趣的是,这个问题也是重复的......).事实上,我不认为我的问题与那个问题有关.它侧重于" void foo()C中的含义",但我知道这意味着"我可以传递任意数量的论据",而且我也知道这是一个过时的功能.

但这个问题完全不同.关键字是"假设".我只是想知道我是否传递了不同数量的参数void foo(),就像上面的示例代码一样,它们可以在里面使用foo()吗?如果是这样,这是怎么做到的?如果没有,传递的参数会有什么不同吗?那是我的问题.

c parameters declaration command-line-arguments function-declaration

10
推荐指数
1
解决办法
188
查看次数

*未指定*在C++ typedef语句中的含义是什么?

我看到像这样的陈述

typedef*unspecified*value_type;

typedef*unspecified*reference;

在Boost :: multi_array类的声明中.

    namespace boost {

template <typename ValueType, 
          std::size_t NumDims, 
          typename Allocator = std::allocator<ValueType> >
class multi_array {
public:
// types:
  typedef ValueType                             element;
  typedef *unspecified*                         value_type;
  typedef *unspecified*                         reference;
  typedef *unspecified*                         const_reference;
  typedef *unspecified*                         difference_type;
  typedef *unspecified*                         iterator;
  typedef *unspecified*                         const_iterator;
  typedef *unspecified*                         reverse_iterator;
  typedef *unspecified*                         const_reverse_iterator;
  typedef multi_array_types::size_type          size_type;
  typedef multi_array_types::index              index;
  typedef multi_array_types::index_gen          index_gen;
  typedef multi_array_types::index_range        index_range;
  typedef multi_array_types::extent_gen         extent_gen;
  typedef multi_array_types::extent_range       extent_range;
  typedef *unspecified*                         storage_order_type;
Run Code Online (Sandbox Code Playgroud)

*未指定*在这里意味着什么?这是C++ 11标准吗?

c++ typedef declaration c++11

9
推荐指数
2
解决办法
3226
查看次数

在GHCi提示符下的声明

我刚刚安装了Haskell Platform for Windows(版本2011.2.0.1),并开始使用HaskellQuestions.pdf

第二个问题需要"x = 3"作为答案.但是当我把它输入GHCi时,我得到了

GHCi, version 7.0.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> x = 3

<interactive>:1:3: parse error on input `='
Prelude>
Run Code Online (Sandbox Code Playgroud)

为什么?我检查了答案,我是对的.这个问题与等号有什么关系?

syntax haskell declaration ghci

9
推荐指数
1
解决办法
708
查看次数

'char(*p)[5];'是什么意思?

人.

我试图理解这三个声明之间的差异:

char p[5];
char *p[5];
char (*p)[5];
Run Code Online (Sandbox Code Playgroud)

我试图通过做一些测试来找到它,因为阅读声明和类似的东西的每个指南到目前为止都没有帮助我.我写了这个小程序并且它不起作用(我已经尝试了其他类型的第三个声明的使用,我已经用完了选项):

#include <stdio.h>                                                              
#include <string.h>                                                             
#include <stdlib.h>                                                             

int main(void) {                                                                
        char p1[5];                                                             
        char *p2[5];                                                            
        char (*p3)[5];                                                          

        strcpy(p1, "dead");                                                     

        p2[0] = (char *) malloc(5 * sizeof(char));                              
        strcpy(p2[0], "beef");                                                  

        p3[0] = (char *) malloc(5 * sizeof(char));                              
        strcpy(p3[0], "char");                                                  

        printf("p1 = %s\np2[0] = %s\np3[0] = %s\n", p1, p2[0], p3[0]);          

        return 0;                                                               
}
Run Code Online (Sandbox Code Playgroud)

第一个和第二个工作没问题,我已经明白他们做了什么.第三个声明的含义是什么,使用它的正确方法是什么?

谢谢!

c pointers declaration

9
推荐指数
1
解决办法
6067
查看次数

在c标准库中定义了stdin?

我在stdio.h中找到了这一行:

extern struct _IO_FILE *stdin;
Run Code Online (Sandbox Code Playgroud)

基于这个'extern'关键字,我认为这只是一个声明.我想知道stdin在哪里定义和初始化?

c stdin declaration definition standard-library

9
推荐指数
1
解决办法
7313
查看次数

c中的嵌套结构

我必须构建一个嵌套结构来存储一些人的某些基本信息(姓名,年龄,地址).所以我创建了一个名为"info"的结构,并保存地址,我在"info"中创建了另一个嵌套结构,名为"address".但每当我提示使用for循环存储值时,我都会收到错误.这里有什么问题,我该如何解决?

[错误]'struct Info'没有名为'address'的成员
[Warning]声明没有声明任何内容[默认启用]

#include <stdio.h>

int main(){

    struct Info{
        char name[30];
        int age;
        struct address{
            char area_name[39];
            int house_no;
            char district[39];
        };
    };

    struct Info Person[10];

    int i;
    for(i=0;i<10;i++){

        printf("enter info of person no %d\n",i);
        printf("enter name\n");
        scanf("%s",&Person[i].name);
        printf("enter age\n");
        scanf("%d",&Person[i].age);
        printf("enter address :\n");
        printf("enter area name :\n");
        scanf("%s",&Person[i].address.area_name);
        printf("enter house no : \n");
        scanf("%d",&Person[i].address.house_no);
        printf("enter district : \n");
        scanf("%s",&Person[i].address.district);
    }
}
Run Code Online (Sandbox Code Playgroud)

c structure declaration

9
推荐指数
2
解决办法
6207
查看次数

多声明中的第二个int是否始终设置为1?

这篇Code Golf帖子中,声称"定义中的第二个变量始终设置为1",这使得它成为一个格式良好的行:

int i=-1,c,o,w,b,e=b=w=o=c;
Run Code Online (Sandbox Code Playgroud)

据说除了i设置为1 之外的所有内容c都是自动1.

我以为我知道一些C,并认为这是非法的(是UB,如果有任何随机堆栈内容的结果).

C真的设置c为1吗?

c declaration definition undefined-behavior variable-declaration

9
推荐指数
2
解决办法
172
查看次数