8

If I have the following code:

struct someStruct
{
  int x;
  int y;
}

class someClass
{
  public:
    someStruct m_member;

    alias      m_x = m_member.x; // I am aware alias is not a keyword and even if it was, I cannot assign it a value like this
    alias      m_y = m_member.y; // I am aware alias is not a keyword and even if it was, I cannot assign it a value like this
}

Can I give aliases to m_member.x and m_member.y without incurring the extra storage cost of a reference (which is the same as a pointer)? Something similar to a typedef?

1
  • 1
    Nod. Such an 'alias' keyword would certainly be valuable when you want to deprecate an old name without breaking existing code or to transparently compose one data structure of another. D pleasantly has the "alias" keyword, and C++ has "using", but it sadly only works for type names rather than field names. I hope C++ someday adopt D's ability (but it would likely be via the "using" keyword instead of "alias"). Jul 24, 2014 at 22:06

2 Answers 2

10

You can write inline access functions:

class someClass
{
public:
  someStruct m_member;
  int& mx() { return m_member.x; }
  int& my() { return m_member.y; }
  int const& mx() const { return m_member.x; }
  int const& my() const { return m_member.y; }
};

Then you can access the members as follows:

someClass foo;
foo.mx() = 3;
foo.my() = 2*foo.mx();

It's not exactly the same syntax, but close, and a reasonably good compiler will completely optimize away the inline functions.

1
  • @TonyDelroy: Thanks for pointing out that omission; I've now added them.
    – celtschk
    Jan 12, 2012 at 9:33
1

In this scenario I would just use a reference as it's what they're suited for. If a reference overhead is really too much I would just forgo the alias and use the full expression.

However you could achieve this with a macro (read: evil)

class someClass
{
  public:
    someStruct m_member;
#define m_x m_member.x
#define m_y m_member.y

  ...

#undef m_x
#undef m_y
};

I would highly recommend not doing this though. Like all macros though it has some unintended consequences (could cause incorrect referencing for other valid uses of m_x within the type).

For example

struct s1 {
  int m_x;
};

class someClass {
public:
  someStruct m_member;
#define m_x m_member.x
#define m_y m_member.y

  ...
  void Example(S1& s1) { 
    s1.m_x = 42;  // This wouldn't compile and you'd get terrible error messages
  }
};
4
  • 1
    Note that if you #undef the macros within the class, clients can't use them. But if you don't #undef them, the result is probably worse. Jan 12, 2012 at 0:52
  • @R.MartinhoFernandes I was trying to limit my hack / macro to the smallest possible area :)
    – JaredPar
    Jan 12, 2012 at 0:53
  • I think if you're going to overload m_x like that, you're looking to get bitten in any case.
    – Carl Norum
    Jan 12, 2012 at 0:53
  • 1
    It's called a "hackro" for a reason.
    – Puppy
    Jan 12, 2012 at 0:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.