Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3602080
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:42:05+00:00 2026-05-18T20:42:05+00:00

I have a struct that represents a vertex. It has x, y and z

  • 0

I have a struct that represents a vertex. It has x, y and z fields as well as several others. Recently I came into conclusion that for certain functionality I will need to access the coordinates of the vertex as an array. I didn’t want to “pollute” the code with temporary variables or change all places that look like this v.y to this v.coord[1] which is not nice nor elegant. So I thought about using a union. Something like this should work:

struct {
  float x,y,z;
} Point;

struct {
    union {
        float coord[3];
        Point p;
    };
} Vertex;

This is good, but not perfect. The point class has no point being there. I want to be able to access y coordinate simply by typing v.y (and not v.p.y).
Can you suggest a hack to solve this (or tell me that it is not possible)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:42:05+00:00Added an answer on May 18, 2026 at 8:42 pm

    A good C++ approach is to use named accessors that return references to the elements:

    class Point {
    public:
        float& operator[](int x)       { assert(x <= 2); return coords_[x]; }
        float  operator[](int x) const { assert(x <= 2); return coords_[x]; }
    
        float& X()       { return coords_[0]; }
        float  X() const { return coords_[0]; }
    
        float& Y()       { return coords_[1]; }
        float  Y() const { return coords_[1]; }
    
        float& Z()       { return coords_[2]; }
        float  Z() const { return coords_[2]; }
    private:
        float coords_[3];
    };
    

    With this approach, given a Point p;, you can use both p[0] and p.X() to access the initial element of the internal coords_ array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple question. I have a struct which has a member that is also a
In my C# application, I have a large struct (176 bytes) that is passed
I have a function that takes a struct, and I'm trying to store its
I have this routine that calculates the seconds-to-date for a struct tm . On
I have a function that takes pointer to pointer to struct. struct ABC; void
I have a fairly simple const struct in some C code that simply holds
I have class with a member function that takes a default argument. struct Class
I have an XHTML strict page that has an invisible div that is controlled
Summary: I have a struct that is read/written to file. This struct changes frequently,
I have a struct with some fields. One of the fields is of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.