草庐IT

objective-c - 在 Objective C 中使用 extern

在ObjectiveC中使用extern有多好?它确实使某些部分的编码变得容易......但它不会破坏面向对象吗? 最佳答案 您会发现extern在Cocoa框架中被广泛使用,并且很难找到一个令人信服的论点,即他们的OO被“宠坏了”。相反,Cocoa被很好地封装并且只暴露它必须的东西,通常是通过extern。全局定义的常量当然是最常见的用法,但不一定是唯一有效的用法。IMO,使用extern不一定会“破坏”面向对象。即使在OO中,也经常使用可从任何地方访问的变量。使用extern是解决Objective-C中缺少“类变量”(如Jav

c - extern 关键字是什么意思?

extern关键字是什么意思?我在像这样的函数声明之前已经看到了externvoidDoFoo... 最佳答案 extern给出了一个名称​​外部链接。这意味着对象或函数可以通过该名称从程序中的其他翻译单元访问。对于函数,这是任何情况下的默认链接,因此它的使用(在此上下文中)通常是多余的。 关于c-extern关键字是什么意思?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/27

c++ - 如何使用外部模板

我一直在浏览C++0x的N3291工作草案。我对外部模板很好奇。第14.7.3节指出:Exceptforinlinefunctionsandclasstemplatespecializations,explicitinstantiationdeclarationshavetheeffectofsuppressingtheimplicitinstantiationoftheentitytowhichtheyrefer.仅供引用:术语“显式实例化声明”是externtemplate的标准说法。.这是在第14.7.2节中定义的。这听起来像是在说如果你使用externtemplatestd::

c++ - 如何使用外部模板

我一直在浏览C++0x的N3291工作草案。我对外部模板很好奇。第14.7.3节指出:Exceptforinlinefunctionsandclasstemplatespecializations,explicitinstantiationdeclarationshavetheeffectofsuppressingtheimplicitinstantiationoftheentitytowhichtheyrefer.仅供引用:术语“显式实例化声明”是externtemplate的标准说法。.这是在第14.7.2节中定义的。这听起来像是在说如果你使用externtemplatestd::

c++ - 为什么我们在 C++ 中需要 extern "C"{ #include <foo.h> }?

我们为什么需要使用:extern"C"{#include}具体来说:我们应该什么时候使用它?需要我们使用它的编译器/链接器级别发生了什么?这在编译/链接方面如何解决需要我们使用它的问题? 最佳答案 C和C++表面上相似,但各自编译成一组非常不同的代码。当您在C++编译器中包含头文件时,编译器需要C++代码。但是,如果它是C头文件,则编译器期望头文件中包含的数据被编译成某种格式——C++'ABI'或“应用程序二进制接口(interface)”,因此链接器会阻塞。这比将C++数据传递给需要C数据的函数更可取。(要深入了解细节,C++的A

c++ - 错误 LNK2019 : unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup

当我运行下面的简单代码时,我有两个错误如下:#include#includeusingnamespace::std;templateclassStack{public:Stack(intmax):stack(newType[max]),top(-1),maxsize(max){}~Stack(void){delete[]stack;}voidPush(Type&val);voidPop(void){if(top>=0)--top;}Type&Top(void){returnstack[top];}//friendostream&operatorvoidStack::Push(Type&

c++ - 什么是 undefined reference / Unresolved external symbol 错误,我该如何解决?

什么是undefinedreference/Unresolvedexternalsymbol错误?什么是常见原因以及如何解决/预防它们? 最佳答案 按照2.2(creditstoKeithThompsonforthereference)的规定,编译C++程序需要几个步骤。:Theprecedenceamongthesyntaxrulesoftranslationisspecifiedbythefollowingphases[seefootnote].Physicalsourcefilecharactersaremapped,inan

c++ - C++中extern "C"的作用是什么?

将extern"C"放入C++代码到底有什么作用?例如:extern"C"{voidfoo();} 最佳答案 extern"C"使C++中的函数名具有C链接(编译器不会破坏名称),以便客户端C代码可以使用C兼容header链接到(使用)您的函数仅包含函数声明的文件。您的函数定义包含在二进制格式(由您的C++编译器编译)中,然后客户端C链接器将使用C名称链接到该格式。由于C++有函数名重载,而C没有,C++编译器不能只使用函数名作为链接的唯一id,因此它通过添加有关参数的信息来破坏名称。C编译器不需要修改名称,因为您不能在C中重载函数

c++ - 为什么模板不能在 extern "C" block 内?

这是ananswer的后续问题。至Isitpossibletotypedefapointer-to-extern-“C”-functiontypewithinatemplate?此代码无法使用g++、VisualC/C++和ComeauC/C++编译,错误消息基本相同:#includeextern"C"{staticintdo_stuff(int){return3;}templatestructtest{staticvoidfoo(return_t_(*)(arg1_t_)){}};}intmain(){test::foo(&do_stuff);returnEXIT_SUCCESS;}

c++ - 为什么模板不能在 extern "C" block 内?

这是ananswer的后续问题。至Isitpossibletotypedefapointer-to-extern-“C”-functiontypewithinatemplate?此代码无法使用g++、VisualC/C++和ComeauC/C++编译,错误消息基本相同:#includeextern"C"{staticintdo_stuff(int){return3;}templatestructtest{staticvoidfoo(return_t_(*)(arg1_t_)){}};}intmain(){test::foo(&do_stuff);returnEXIT_SUCCESS;}