用关键字声明外部本地变量
Declare an external local variable with a keyword?
我想理解为什么C 不能提供一个关键字来声明称为函数的变量,该函数本地为呼叫函数。实际上,我需要继承一个向量类,并且必须定义通常的操作:
template <unsigned int N>
class Vector
{
public:
Vector(const std::array<float, N>& coords);
Vector<N>& operator*=(float k);
// others...
protected:
std::array<float, N> m_coords;
};
class Vector3 : public Vector<3>
{
public:
Vector3(float x = 0.f, float y = 0.f, float z = 0.f);
// some specific operations like cross product
}
template <unsigned int N>
Vector<N> operator*(float k, const Vector<N>& a)
{
Vector<N> res(a);
res *= k;
return res;
}
如果我返回对新对象的引用,则此类函数将适用于每个继承的向量
template <unsigned int N>
Vector<N>& operator*(float k, const Vector<N>& a)
{
Vector<N>* res = a.getClone();// virtual method returning a new object
*res *= k;
return &res;
}
但是,我不想在调用函数中删除res,所以我想创建一个呼叫函数的局部变量。
可能?
对不起,我忘记了外部功能的模板方法:
template <class T>
T operator*(float k, const T& a)
{
T res(a);
res *= k;
return res;
}
因此,我可以用vector3。
我没有回答关键字问题。
相关文章:
- 在类函数中初始化外部作用域变量
- 如果全局变量默认是外部变量,为什么要添加"extern"关键字?
- 同时具有"外部"和"内联"说明符的变量
- 访问 PlatformIO 中的外部环境变量
- 如何在函数外部访问函数中局部变量的值?
- 堆栈/帧指针作为外部变量
- 使用外部文件中的变量进行视觉C++
- 内联函数/变量的外部链接
- 当使用lambda进行变量的复杂初始化时,如何处理从内部抛出的lambda外部异常
- const_cast const 方法中的"this"将"this"分配给外部变量?
- 全局外部指针变量在 DLL 中不可见
- 在没有外部文件的情况下在应用会话之间保存变量
- 外部变量的初始化
- 全局变量的循环依赖性与外部说明符
- 如何在命名空间中初始化外部变量
- 声明类指针变量外部班级
- 在 VC++ 中访问一个文件中声明的变量.(外部关键字用法)
- 为什么 cout 不打印变量"C"外部?
- 全局变量外部声明后的线程专用指令
- c++变量外部声明