5

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

class vector2
{
  int x, y;
}

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.

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.

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)

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

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();}
}

but where I can just write something like

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

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.

thecoshman
  • 8,394
  • 8
  • 55
  • 77
  • 4
    Are you sure you want to use the same type for positions as well as dimensions, just because they happen to be *structurally* the same? Sounds like a bad idea to me. I would define two distinct types. This makes the intentions of client code much clearer. – fredoverflow Nov 14 '10 at 15:09
  • 1
    "I am not asking about the mechanics of inline functions...", maybe not... but inline functions does what you want to do... – ronag Nov 14 '10 at 16:05

6 Answers6

4

but the compiler might decide to not inline these functions

Then the compiler probably has a good reason to do so.

I think this is a non problem, just have the function with the alternative name call the "real" function, and the compiler will most likely inline it.

EDIT:

If that didn't convince you, it is possible to use __forceinline in visual studio. Here is the way to force inline in GCC.

EDIT2:

class MyBetterClass
{
  public:
  void foo(){ /* BLOCK OF CODE */ }
  __forceinline void bar(){ foo(); /* DO WHAT EVER foo() DOES */ }
}
ronag
  • 49,529
  • 25
  • 126
  • 221
3

Using C++11 you could do:

//works for non-template non-overloaded functions:

const auto& new_fn_name = old_fn_name;

Other solutions: How do I assign an alias to a function name in C++?

Community
  • 1
  • 1
jave.web
  • 13,880
  • 12
  • 91
  • 125
1

Your referenced link is AFAIK not a C feature either, but something specific to that particular compiler.

C++ provides such a mechanism: it happens to be inlined functions! Worrying about the compiler not optimizing away the redundant call in an inlineable function is definitely premature optimization. Inline, then measure if you're worried about performance.

If you're absolutely insisting on eliminating the merest chance of a redundant call, you might do something with preprocessor #defines... but beware: macros do not respect class boundaries, and your header files will sooner or later stomp on some other, unrelated code. Better not go there.

Pontus Gagge
  • 17,166
  • 1
  • 38
  • 51
  • Don't forget that #define-inline'd is a pain to debug! – mmmmmmmm Nov 14 '10 at 15:43
  • @rstevens: indeed. I can hardly recommend it, but it is a possible if more complicated way of achieving what you can do more elegantly with inlined functions. If, indeed, you really want to add **apparent** complexity (several member function names) where it doesn't really exist. – Pontus Gagge Nov 15 '10 at 11:49
1

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.

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.

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.

0

you could use preprocessor #defines.... if you're into the world's worst bugs. You'll get the equivalent of guaranteed inline if you want, or just aliases if you want that too.

rtpg
  • 2,419
  • 1
  • 18
  • 31
-3

So, you want to have two functions, on the same object, that return exactly the same data, as the same data type.

Don't DO that.

Providing more than one path to the same data is one of those things that sounds like it might be convenient for whoever's going to be using your object-- until you think about it. What happens is that six months down the road, somebody turns up a bug in one of the two functions, and you fix that function but not the other one, so the bug's still there. Or the programmer who's writing clients for your object is driven half-insane wondering what the difference is between getLength() and getSpan().

The one time I'd do this would be when implementing an interface that requires a duplicate of an existing member function. In that case, the interface's function is going to be virtual, so the notion of inlining goes out the window.

mjfgates
  • 3,351
  • 1
  • 18
  • 15
  • 5
    Except I wan to only write the function once and give multiple names to it call it with. So if `foo` had a bug and it was fixed, because `bar` is the the same function it is fixed too – thecoshman Nov 14 '10 at 15:41
  • Misinterprets the question, offering non-sequitur advice against it as a practice. – Syndog May 19 '16 at 18:49