在c ++中给一个函数实现多个名称 [英] Giving a function implementation more than one name in c++

查看:197
本文介绍了在c ++中给一个函数实现多个名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个基本的2D向量类,例如

Let say I have a basic 2D vector class something like

class vector2
{
  int x, y;
}

这两个值可用于表示位置以及宽度和高度。 C ++为我提供了一个函数,如 vector2 :: getXpos(),然后还定义 vector2 :: getWidth() code>并且使用相同的实现。

these two values could be used to represent a position as well as a width and height. does C++ provide a away for me to impliment a function such as vector2::getXpos() and then also define vector2::getWidth() and have it use the same implementation.

我知道我可以只做这两个函数内联,但编译器可能决定不内联这些功能。所以如果 getWidth 只是调用 getXpos ,最终会有两个函数调用。

I know that I could just make both of these function inline, but the compiler might decide to not inline these functions. so if getWidth just called getXpos you would end up with two function calls.

一个更复杂的例子,我想使用这是为 getLength()和erm ... getSpan (想像这里的屏幕,当你说40电视)

A more relistic example of what I would want to use this for is getLength() and erm... getSpan() (thinking of like a screen here for when you say 40" tv)

我会假设这将是一个简单的情况下,一个特殊的函数定义...我找到了这个页面,但这听起来像是一个C功能...和一个黑客来工作。

I would assume that this would be a simple case of something like a special function definition... I did find this page but this sounds like it is a C feature... and a bit of a hack to get working.

EDIT

我不是在询问内联函数的机制...我基本上想做一些功能, / p>

I am not asking about the mechanics of inline functions... I basicaly want to do something functionally like

class MyClass
{
  void ActaullyDoStuff();
  public:
  void foo(){ActaullyDoStuff();}
  void bar(){ActuallyDoStuff();}
}

但是我可以写下像

class MyBetterClass
{
  public:
  void foo(){ /* BLOCK OF CODE */ }
  void bar(){ /* DO WHAT EVER foo() DOES */ }
}

我想要 bar() code> foo(),以便相同的功能代码根据情况可以有不同的,更合适的名称。

I want bar() to be another way of just doing foo() so that the same functional code can have different, more appropriate names depending on the situation.

推荐答案

似乎你没有以面向对象的方式来思考这个问题。我必须第二个mjfgates建议,你真的不想这样做。

It appears you're not thinking about this in an object oriented way. I have to second mjfgates advice that you really don't want to do this.

你想做的是将一个向量的想法抽象成一个类,并实现可能想要用于向量的常用方法。事实上,你可能想考虑将上面的类作为一个Point类来实现,然后使用一个Vector类聚合两个点类。

What you want to do is abstract the idea of a vector into a class and implement the common methods you might want to use with a vector. In fact, you may want to consider implementing your class example above as a "Point" class and then have a "Vector" class aggregate two point classes.

,你的类将不会被很好地定义,如果它被用于两个不同的目的。让我们说,你想在一些类上绘制一个方法来绘制vector2。你必须知道vector2的哪些实例代表一个起点,哪些实例代表一个宽度/高度。你可能还需要第三个表示来表示方向。更简单的方法是根据getStartPoint,getEndPoint和任何其他方法来实现向量,这些方法将对向量执行适当的计算。然后消费者不需要知道vector2类的内部工作,他们只是调用方法来获得他们需要的信息。

Using your example, your class would not be well defined if it was used for two different purposes. Let's say you want to make a method on some class to draw vector2. You would have to know which instances of vector2 are representing a starting point and which ones are representing a width/height. You'd probably also need a third representation to represent direction. The easier way is to implement the vector in terms of getStartPoint, getEndPoint, and any other methods that will do calculations appropriate for the vector. Then the consumer doesn't need to know about the internal working of the vector2 class, they just call the methods to get the information they need.

这篇关于在c ++中给一个函数实现多个名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆