-1

Is there an equivalent to Delphi's absolute in C++? I want to do the following:

// Structure A
struct A
{
  double X;
  double Y;
  double Z;
}

// Structure B
struct B : A
{
  double U absolute X;
  double V absolute Y;
  double W absolute Z;
}

I can use double & U = X in structure B but this will generate an additional pointer and change the size of the structure. Union is - I think - also not a solution, because structure B inherits structure A.

What I really want is to have access to the same memory by accessing for example X or U.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 2
    What exactly is the benefit of this? Why don't you go with functions that return a reference to the member you are "hiding"? `double & X(){return A;}` – Simon Kraemer Dec 14 '16 at 14:32
  • The problem is that we have several kinds of coordinates. Some of them are named x, y, z others (in another system) are named u, v, w. To keep the code understandable we need to name them correctly. Also structure A has a lot of functionality. I want to have this functionality also for structure B. – Alexander Vogel Dec 14 '16 at 14:35
  • @Simon: Getter and setter could do the trick. You're right :-). But there is the problem that I cannot access them like a standard member. I have to give different names to the getter and the setter and I cannot directly assign values. I have to access them via U() and set_U(value) for example... – Alexander Vogel Dec 14 '16 at 14:40
  • You don't even need getters and setters - just return a non-const reference. Assume my example would be part of `struct C : A`. You could write something like `C c; c.X() = 5;` Example: https://godbolt.org/g/Oo0oab – Simon Kraemer Dec 14 '16 at 14:46
  • Please pick one language. C or C++. Which is it? Don't think for a minute that this is one language. Finally, present the problem rather than your solution. Other the answer is a simple no which won't help you much. – David Heffernan Dec 14 '16 at 14:48
  • 3
    Not only is this not possible in C++, it is not possible in Delphi. You cannot use `absolute` anywhere other than when declaring a local variable or a global variable. You absolutely cannot use `absolute` when declaring a field in a record or class. – David Heffernan Dec 14 '16 at 14:55
  • @AlexanderVogel **IF** you are using C++Builder specifically, you can use properties to give you field-like syntax, as long as you don't need to access the memory address of the fields, eg: `struct B : A { __property double U = {read=X, write=X}; ... }; B b; b.U = 5;` – Remy Lebeau Dec 14 '16 at 16:19
  • @DavidHeffernan in Delphi it would be impossible long before - records are not classes in Delphi, so no inheritance. But in Delphi there would be properties in records, so having pseudonyms for variables is trivial. I think in C++ you can have INLINEd getter/setter procedures ,giving essentially the same thing. – Arioch 'The Dec 14 '16 at 16:48
  • @Arioch'The You could remove the inheritance and merge the types, but you still could not get `absolute`. For sure there are other ways to achieve the goals, but the question was very specifically about `absolute`. A classic case of the asker asking about the solution rather than the problem. – David Heffernan Dec 14 '16 at 16:56
  • ESR's smart question essay should be re-enabled on the SO. As harsh as it worded it is absolutely true to the last word. It is like with the proverbial "non-standard in-complete LISP interpreter", we basically have time and again to found politically correct ad hoc incomplete ways to rephrase ESR. – Arioch 'The Dec 14 '16 at 17:03
  • 1
    not even about solution but about implementation details of the solution. Kind of "i mastered hammering nails in. Now which hammer can use and how to hammer a screw" – Arioch 'The Dec 14 '16 at 17:06
  • Additionally, C++ has analogue of `absolute` ( of real one, not the imaginary keyword of this Q ). Just a year ago i ported snippets full of `int x; double const * y = &x;` . And if &-reference knows the tricks *-pointers do it would be an absolute copy of it – Arioch 'The Dec 14 '16 at 17:15

3 Answers3

3

I like the idea of user2079303's answers, but want to inverse it.

Note - the question is named wrong and is violating "what is your real goal" or "show me your Z" rule. We do not need to mimic absolute keyword - it is total nonsense! we want to make pseudonyms for the record properties and use them interchangeably.

What I really want is to have access to the same memory by accessing for example X or U.

The quoted line is the ONLY line in the question that talks about the problem essence - making full pseudonyms.

So, let's start where user2079303 stopped and use the fact that C's unions do not have to be named, like it was used in https://stackoverflow.com/a/13624921/976391

So we just inverse the scopes.

/*union*/ struct coordinates {
    /*struct*/ union {
        double X;
        double U;
    };
    /*struct*/ union {
        double Y;
        double V;
    };
    /*struct*/ union {
        double Z;
        double W;
    };
};
Community
  • 1
  • 1
Arioch 'The
  • 15,799
  • 35
  • 62
  • Ah, this is much nicer than my suggestion! – eerorika Dec 14 '16 at 17:37
  • @user2079303 I know very little about c++, for example I wonder if inner structs can be anonymous too, uplifting their members up the namespaces. Will it even compile if you strike out two identifiers in your answer, for example? – Arioch 'The Dec 14 '16 at 17:44
  • Structs (classes) may not be anonymous in C++, like they may in C (although some compilers support it as language extension). Anonymous member unions are allowed. So, striking out the name would make my answer as usable as this but make it only valid in C. – eerorika Dec 14 '16 at 17:49
  • Yeah... They had to allow one of anonymouses to survive for this trick to be workable. They chosen another one... – Arioch 'The Dec 14 '16 at 17:54
  • And I wonder if `union {double X,U;}` would be the same or would be a degenerate one-option union instead. – Arioch 'The Dec 14 '16 at 17:59
  • `double X,U;` is semantically equivalent to `double X; double U;`, whether it is inside a struct or a union. – eerorika Dec 14 '16 at 21:12
  • So I could use it. I just was not sure, so opted to be on a safe while noisy and redundant side. – Arioch 'The Dec 15 '16 at 03:34
2

Short version: There is no equivalent of the described language feature.


Long version C++:

There isn't an equivalent in C++. The member reference that you suggest gives you the same syntax, but indeed increases the size of the object.

Another close alternative is a member function, that returns a reference. That has no overhead (assuming inline expansion). A function call has different syntax than referencing a member though. But perhaps having identical syntax with member access is not important so this is what I suggest.


Long version C:

There is no inheritance in C at all, so there is nothing like the described "absolute" in it either.

we have several kinds of coordinates. Some of them are named x, y, z others (in another system) are named u, v, w.

Given this context, I would suggest a union (EDIT: It's better to swap the union and struct relation, see https://stackoverflow.com/a/41148089/2079303):

union coordinates {
    struct {
        float x, y, z;
    } xyz;
    struct {
        float u, v, w;
    } uvw;
};
Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • I would prefer making the members of `A` protected and just provide correctly named member functions returning a reference to these members in the inherited classes/structs. – Simon Kraemer Dec 14 '16 at 14:38
  • Using a c union is equivalent to a Delphi variant record. `Type A = record case boolean of false: (x,y,z: Double); true: (u,v,w: Double); end;` There is no possibility in Delphi to use the absolute keyword inside a record or a class. – LU RD Dec 14 '16 at 15:12
  • you ALMOST did it :-) – Arioch 'The Dec 14 '16 at 16:56
0

If you want to interchangeably use XYZ and UVW, you can let each convert to the other.

// forward declare
struct B;

struct A
{
  double X;
  double Y;
  double Z;
  operator B() const;
}

struct B
{
  double U;
  double V;
  double W;
  operator A() const;
}

A::operator B() const { B b; b.U = X; b.V = Y; b.W = Z; return b; }
B::operator A() const { A a; a.X = U; a.Y = V; a.Z = W; return a; }
Caleth
  • 52,200
  • 2
  • 44
  • 75