style cast" in g++

Possible Duplicate: When should static_cast, dynamic_cast and reinterpret_cast be used? With this C++ code, char* a = (char*) b; I got warning warning: use of old-style cast . What would be the new-style cast? reinterpret_cast , static_cast , dynamic_cast and const_cast are the c++ cast alternatives. const_cast to remove const/volatile from a const variable. dynamic_cast to perform

风格转换“在g ++中

可能重复: 什么时候应该使用static_cast,dynamic_cast和reinterpret_cast? 有了这个C ++代码, char* a = (char*) b; 我收到警告warning: use of old-style cast 。 什么是新式演员? reinterpret_cast , static_cast , dynamic_cast和const_cast是c ++ const_cast的替代品。 const_cast从const变量中删除const / volatile。 dynamic_cast在多态类型之间进行转换时执行运行时有效性检查 static_cast在继承

Where to put default parameter value in C++?

This question already has an answer here: Default value of function parameter 4 answers Default parameter values must appear on the declaration, since that is the only thing that the caller sees. EDIT: As others point out, you can have the argument on the definition, but I would advise writing all code as if that wasn't true. You can do either, but never both. Usually you do it at fu

在C ++中放置默认参数值的位置?

这个问题在这里已经有了答案: 函数参数4的默认值应答 默认参数值必须出现在声明中,因为这是调用者看到的唯一东西。 编辑:正如其他人指出的,你可以在定义上有参数,但我会建议编写所有代码,就好像这不是真的。 你可以做,但从来都没有。 通常你在函数声明中做,然后所有的调用者都可以使用这个默认值。 但是,您可以在函数定义中做到这一点,然后只有看到定义的人才能够使用默认值。 最有用的地方在于声明(.h)

Does std::malloc return NULL or nullptr on failure

According to http://en.cppreference.com/w/cpp/memory/c/malloc std::malloc returns a null ptr on failure Is that a NULL pointer or a nullptr? If it is nullptr, that implies there is a difference between std::malloc and the C malloc. Then the other question that follows, in that case, are there any other differences? EDIT: This is not a duplicate as suggested in the comment. That explains w

std :: malloc在失败时返回NULL或nullptr

根据http://en.cppreference.com/w/cpp/memory/c/malloc std :: malloc在失败时返回一个空ptr 那是一个NULL指针还是一个nullptr? 如果它是nullptr,那意味着std :: malloc和C malloc之间有区别。 那么接下来的另一个问题是,在那种情况下,还有其他的区别吗? 编辑:这是不重复的建议在评论中。 这解释了nullptr是什么,明确地说它们是不同的。 问题不在于询问它们之间的区别。 它返回NULL 。 从C ++标准的N4296草

Why does C++ require a cast for malloc() but C doesn't?

I have always been curious about this - why do in C++ I have to cast return value from malloc but not in C? Here is the example in C++ that works: int *int_ptr = (int *)malloc(sizeof(int*)); And here is the example in C++ that doesn't work (no cast): int *int_ptr = malloc(sizeof(int*)); I heard that in C, in fact, casting an output from malloc() is a mistake. Can anyone comment on thi

为什么C ++需要对malloc()进行强制转换,但C不是?

我一直对此感到好奇 - 为什么在C ++中我必须从malloc转换返回值,但不能在C中转换? 这是C ++中的例子: int *int_ptr = (int *)malloc(sizeof(int*)); 以下是C ++中不起作用的示例(无法投射): int *int_ptr = malloc(sizeof(int*)); 我听说在C中,实际上,从malloc()输出一个输出是一个错误。 任何人都可以评论这个话题吗? 几点: C允许void指针隐式转换为任何其他对象指针类型。 C ++没有。 如果忘记包含st

malloc casting error

Everyones suggesting not to cast while allocating a pointer here, do I cast result of malloc But my below non-casted code produce compiler error in VS-2013. Why! #include <stdio.h> #include <malloc.h> int main(){ int *ptr = malloc(sizeof(int) * 100); // compiler error return 0; } Compiler error is, 1 IntelliSense: a value of type "void *" cannot be used to i

malloc转换错误

大家建议不要在这里分配一个指针时进行投射,我会投出malloc的结果 但是我下面的非铸造代码会在VS-2013中产生编译器错误。 为什么! #include <stdio.h> #include <malloc.h> int main(){ int *ptr = malloc(sizeof(int) * 100); // compiler error return 0; } 编译器错误是, 1智能感知:类型“void *”的值不能用于初始化类型为“int *”的实体 另一个问题的建议仅限于C 。 在C++ ,您需要强制

malloc assigned to pointer

I have this problem with C. I have the following statement. int *a; a=malloc(100); And I get the following error: error: invalid conversion from 'void*' to 'int*' [-fpermissive] Any hints on this ? You are compiling your code as C++, in which the code you've used is not valid. For C though, it is valid and you should not add any cast. Note, however, that the argum

malloc分配给指针

我有这个问题与C. 我有以下声明。 int *a; a=malloc(100); 我得到以下错误: 错误:从'void *'无效转换为'int *'[-fpermissive] 任何提示? 您正在将代码编译为C ++,其中您使用的代码无效。 对于C来说,它是有效的,你不应该添加任何强制转换。 但是请注意, malloc()的参数在char s中,所以“100”有点随机。 如果你想要100个整数,那么: a = malloc(100 * sizeof *a); 当用C ++编写C兼容代码

Malloc or Calloc and when

Possible Duplicate: c difference between malloc and calloc Is calloc same as malloc with memset?? or is there any difference char *ptr; ptr=(char *)calloc(1,100) or char *ptr; ptr=(char *) malloc(100); memset(ptr,0,100); This is how calloc is defined by gcc: PTR calloc (size_t nelem, size_t elsize) { register PTR ptr; if (nelem == 0 || elsize == 0) nelem = elsize = 1;

Malloc或Calloc以及何时

可能重复: malloc和calloc之间的区别 calloc与memoc和memset相同吗? 或者有什么区别 char * ptr; ptr =(char *)calloc(1,100) or char * ptr; ptr =(char *)malloc(100); memset的(PTR,0100); 这就是calloc由gcc定义的方式: PTR calloc (size_t nelem, size_t elsize) { register PTR ptr; if (nelem == 0 || elsize == 0) nelem = elsize = 1; ptr = malloc (nelem * elsize);

C++ like vs C like casts?

Possible Duplicate: Regular cast vs. static_cast vs. dynamic_cast i've been using C-like casts since i've been programming: class* initializedClassInstance; void* test = (void*) initializedClassInstance; and i've been told somewhere that i should get used to C++ casts (static_cast, dynamic_cast...). Is there a reason to prefer one over the other (C++ over C style)? There is a

像C vs C类似的强制转换?

可能重复: 定期投射vs. static_cast vs. dynamic_cast 自从我编程以来,我一直在使用类C的演员阵容: class* initializedClassInstance; void* test = (void*) initializedClassInstance; 而且我被告知某处我应该习惯C ++类型转换(static_cast,dynamic_cast ...)。 是否有理由相对于其他(C ++ over C风格)? 静态演员和动态演员之间有区别,对吧? 但是它是什么? 谢谢! C风格的演员阵容是不安全的。 C +

C++ type casting

Possible Duplicate: When should static_cast, dynamic_cast and reinterpret_cast be used? Until a few days ago, I've always used C style type casting in C++ because it seemed to work good. I recently found out that using C in C++ is very bad.. I've never really used C++ casting before, so I'm wondering if someone could tell me (in their own words preferably) what the difference b

C ++类型转换

可能重复: 什么时候应该使用static_cast,dynamic_cast和reinterpret_cast? 直到前几天,我总是在C ++中使用C风格类型转换,因为它似乎很好。 我最近发现在C ++中使用C非常糟糕.. 我以前从来没有真正使用C ++投射,所以我想知道是否有人可以告诉我(用他们自己的话来说)static_cast,reinterpret_cast和const_cast之间的区别是什么? const_cast我知道从某个东西中删除了一个“const”,但我不确定它们之间的区别,以

cast<int>(x) instead of (int)x?

I've heard that the static_cast function should be preferred to C-style or simple function-style casting. Is this true? Why? The main reason is that classic C casts make no distinction between what we call static_cast<>() , reinterpret_cast<>() , const_cast<>() , and dynamic_cast<>() . These four things are completely different. A static_cast<>() is usuall

cast <int>(x)而不是(int)x?

我听说static_cast函数应该优先于C风格或简单的函数式风格。 这是真的? 为什么? 主要原因是经典的C语言转换对我们所称的static_cast<>() , reinterpret_cast<>() , const_cast<>()和dynamic_cast<>()没有区别。 这四件事情完全不同。 static_cast<>()通常是安全的。 在语言中有一个有效的转换,或者使其成为可能的适当的构造函数。 唯一有点风险的是当你抛弃一个继承的类; 您必须确