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

  • SEARCH
  • Home
  • 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 9143897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:08:13+00:00 2026-06-17T10:08:13+00:00

The short version of this question: Does a pointer to the first data member

  • 0

The short version of this question: Does a pointer to the first data member of a class result in a pointer to all its data members?

Discussion:

I’m reading some sample code, and this is interesting. When a particular function (in this case the glUniformMatrix4fv C function from OpenGL) takes as a parameter an array, this is passed in the common C way of using a pointer to the address of the array’s first element, as follows:

glUniformMatrix4fv(glvariable, 1, 0, &newmatrix[0]); // referring to newmatrix

This developer has a C++ class for creating a 4X4 matrix, for a total of 16 floats. However, the data members of that class are divided into 4 separate vectors of 4 data members each:

Here are the data members of the class:

vec4 x;
vec4 y;
vec4 z;
vec4 w;

The vec4 class in turn provides these data members:

T x;
T y;
T z;
T w;

He then creates a function to point to just the first vec4 x data member:

    const T* Pointer() const
{
    return &x.x;
}

And somehow, this miraculously translates into sending all 4 vec4 objects and their respective 4 components:

glUniformMatrix4fv(modelviewUniform, 1, 0, modelviewMatrix.Pointer());

I’m fairly new to C++ but this surprised me that you can point to just one data member and expect all the rest to get sent as well. Is it because the class defines them in a particular order? What if they had been defined in a different order in the class definition?

  • 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-06-17T10:08:14+00:00Added an answer on June 17, 2026 at 10:08 am

    This is the C++ object model. You’re guaranteed that within the same access level (private/public/protected), the object properties are in order (C++11), and the same for members without access specifiers between them (in C++03).

    This doesn’t mean what you’re doing is a good idea. It’s better to avoid the cast and just have the function do some more work.

    C++11:

    9.2 Class members [class.mem]

    14) Nonstatic data members of a (non-union) class with the same access control (Clause 11) are allocated so
    that later members have higher addresses within a class object. The order of allocation of non-static data
    members with different access control is unspecified (11)
    . Implementation alignment requirements might
    cause two adjacent members not to be allocated immediately after each other; so might requirements for
    space for managing virtual functions (10.3) and virtual base classes (10.1).

    C++03

    9.2 Class members [class.mem]

    12) Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so
    that later members have higher addresses within a class object. The order of allocation of non-static data
    members separated by an access-specifier is unspecified (11.1)
    . Implementation alignment requirements might
    cause two adjacent members not to be allocated immediately after each other; so might requirements for
    space for managing virtual functions (10.3) and virtual base classes (10.1).

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

Sidebar

Related Questions

This is basically a rails 3 version of this question . Short of parsing
The short version of the question - why can't I do this? I'm restricted
Short Version I want to extend SoapClient so it does this internally when accessing
Short Version : What does this (hex?) code found in onesixtyone mean? char object[]
Inspired by this question . Short version: Why can't the compiler figure out the
So the short version of this is: Can I traverse only the elements within
Short version : echo testing | vim - | grep good This doesn't work
So the wonderful low down on this doozie of a problem: short version: We
Short Version: I'm looking for a technique to keep nearly-sorted data in nearly-sorted order
The short question is: if I hg rollback , how does Peter get my

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.