我正在开发一款游戏并且有一个有趣的问题.我有一些游戏范围的常量值,我想在一个文件中实现.现在我有这样的事情:
constants.cpp
extern const int BEGINNING_HEALTH = 10;
extern const int BEGINNING_MANA = 5;
Run Code Online (Sandbox Code Playgroud)
constants.hpp
extern const int BEGINNING_HEALTH;
extern const int BEGINNING_MANA;
Run Code Online (Sandbox Code Playgroud)
然后文件只是#include"constants.hpp"这很好用,直到我需要使用其中一个常量作为模板参数,因为外部链接的常量不是有效的模板参数.所以我的问题是,实现这些常量的最佳方法是什么?我担心简单地将常量放在头文件中会导致在每个翻译单元中定义它们.而且我不想使用宏.
谢谢
我知道你可以通过使用"外部"定义在Objective-C的全局变量,但我意识到,我已经在我的.m文件的顶部我的第一个方法之前声明的变量也是偶然全球(这是造成一些问题).我将它们移动到我的头文件的@interface部分,我认为它正确地声明它们只存在于类中,这解决了我的一些问题,但我仍然有点困惑.
将变量声明为extern并将其放在.m文件的顶部有什么区别?或者那些导致同样的事情?
假设我只想通过传递一个函数指向该函数来从我的一个文件中公开一个函数.声明该函数是否安全static?编译器是否允许进行任何使我的函数指针无效的柔道,或者在该文件的上下文之外使其无意义,因为该函数被声明为特定于该文件?
不是我的代码,而是一个(愚蠢)我的意思的例子:
void static cool_function(void);
void extern (*cool_function_ptr)(void); // Actually, I’m not sure of where the `extern` goes in a function-
// pointer declaration. Damn you, confusing function-pointer syntax!
Run Code Online (Sandbox Code Playgroud)
鉴于该代码(或其语法正确的近似),cool_function_ptr从另一个文件访问是否非法?
请考虑以下代码:
#include <iostream>
using namespace std;
extern "C"
void foo( void );
namespace A
{
template< int No >
class Bar
{
private:
friend void ::foo( void );
static void private_func( int n );
};
template< int No >
void Bar< No >::private_func( int n )
{
cout << "A:Bar< " << No << ">::private_func( " << n << " )" << endl;
}
}
extern "C"
void foo( void )
{
A::Bar< 0 >::private_func( 1 );
}
int main( …Run Code Online (Sandbox Code Playgroud) 我不知道为什么这会让我疯狂,但确实如此.我在main中定义了一个函数并且声明了forward.
static void myFunc(int x);
static void myFunc( int x)
{
//do stuff
}
main()
Run Code Online (Sandbox Code Playgroud)
我想在另一个类中使用myFunc(int x).所以我认为我所要做的就是在该类头中使用extern static void myFunc(int x),然后在类定义中调用它我需要的地方,但它不起作用.
我究竟做错了什么?
谢谢
我想在API中提供一个字符串常量,如下所示:
extern const char* const SOME_CONSTANT;
Run Code Online (Sandbox Code Playgroud)
但是,如果我在我的静态库源文件中定义它
const char* const SOME_CONSTANT = "test";
Run Code Online (Sandbox Code Playgroud)
我在链接到该库并使用SOME_CONSTANT时遇到链接器错误:
错误1错误LNK2001:未解析的外部符号"char const*const SOME_CONSTANT"(?SOME_CONSTANT @@ 3QBDB)
从extern const char* const声明和定义中删除指针const-ness(第二个const关键字)使其工作.如何使用指针常量导出它?
我有以下源代码,我感兴趣.
#include <stdio.h>
extern int foo;
int foo = 32;
int main()
{
printf("%d", foo);
}
Run Code Online (Sandbox Code Playgroud)
这是一段非常正常的代码,当我编译它时
gcc -Wall -Wextra -pedantic foo.c
Run Code Online (Sandbox Code Playgroud)
我没有得到任何警告.
这似乎很奇怪,因为变量既被定义为外部变量又被定义为同一文件中的全局变量.我很确定链接器很容易在同一个文件中找到外部变量的引用,但它看起来不像编码错误吗?如果是这样,为什么编译器不会对此发出警告?
如何在Python中使用外部变量,比如extern int x;在C中?
例如,
main1.py:
from myfunc import print_a
a = 10
print a
print_a()
Run Code Online (Sandbox Code Playgroud)
myfunc.py:
def print_a():
global a
print a
Run Code Online (Sandbox Code Playgroud) 我问自己为什么以下代码有效以及说明符extern在实例化时的作用baz_instance:
struct baz {
int value;
};
extern const baz baz_instance = {3};
template<baz const& b>
int foo(){
return b.value;
}
int main(){
foo<baz_instance>();
return 1;
}
Run Code Online (Sandbox Code Playgroud)
为什么上面的代码首先编译,如果extern省略说明符,为什么不编译?什么是extern符在这个例子吗?
我正在努力加快GLM(OpenGL数学)的编译时间.GLM大量使用C++模板.
这是我到目前为止所尝试的.
math.h
#pragma once
#include <glm\glm.hpp>
extern template struct glm::tvec3<float, glm::highp>;
Run Code Online (Sandbox Code Playgroud)
math.cpp
#include "math.h"
template struct glm::tvec3<float, glm::highp>;
Run Code Online (Sandbox Code Playgroud)
然后我有三个使用glm::vec3模板的文件,glm::vec3是一个typedef glm::tvec3<float, glm::highp>.这三个文件a,b,c看起来几乎相同:
a.cpp, b.cpp, c.cpp
#include "math.h"
glm::vec3 func() {
glm::vec3 a = glm::vec3{1,1,1};
glm::vec3 b = glm::vec3{1,1,1};
return a + b;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用显式实例化定义和显式实例化声明.所以文件a,b,c不应该导致隐式实例化.但编译时间和我不这样做的时间相同.