简体   繁体   English

C++ 如何用不同的名字呼叫一个Function?

[英]C++ How To Call A Function With Different Name?

In c++, I want to want to make a wrapper class. I am using a math library called glm.在 c++ 中,我想做一个包装器 class。我正在使用一个名为 glm 的数学库。 There are classes in there called glm::mat4 for matrix math and glm::vec3 for vector math.那里有用于矩阵数学的 glm::mat4 类和用于矢量数学的 glm::vec3 类。 What I want to do is make a class called Vector which can do everything glm::vec3 can.我想做的是制作一个名为 Vector 的 class,它可以完成 glm::vec3 可以做的一切。

The problem is, lets say I want to create a wrapper or extra layer over glm::vec3, for my Vector class I would have to rewrite all the functions of vec3 and then inside those function I call the glm::vec3 functions (sort of how any wrapper would work).问题是,假设我想在 glm::vec3 上创建一个包装器或额外层,对于我的 Vector class,我将不得不重写 vec3 的所有函数,然后在那些 function 中我调用 glm::vec3 函数(排序任何包装器如何工作)。

So my question is there a way to avoid this cumbersome work?所以我的问题是有没有办法避免这种繁琐的工作? Is there a way I can use the glm::vec3 as it is but just call Vector instead (name change)?有没有一种方法可以按原样使用 glm::vec3 而只是调用 Vector (名称更改)? Is this even possible???这甚至可能吗???

You can alias your type like this:您可以像这样为您的类型起别名:

namespace glm {

class vec3 {
// your implementation...
};

} // namespace glm

// create an alias for glm::vec3
using Vector = glm::vec3;

And after, using Vector will be just like using glm::vec3之后,使用Vector就像使用glm::vec3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM