473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

access to member array via public members

Hi,

I would like to initialize a member array of 3 floats with a memcpy
(for reasons of efficiency). Later I would like to access the floats
via public members (e.g. foo.x, foo.y, foo.z).

Is this possible? (will post ideas in a second)

-Jimmy

Jul 23 '05 #1
21 1647
public:
// add some compiler directive to tightly pack
float x;
float y;
float z;

// later on use memcpy(&foo.x, p_float_data, 3*sizeof(float))

Would this work? How do I tell the compiler to tightly pack? would the
memcpy cause a runtime error?

-jimmy

Jul 23 '05 #2
jimmy wrote:
I would like to initialize a member array of 3 floats with a memcpy
(for reasons of efficiency).
Whatever your reasons, they are probably important to you.
Later I would like to access the floats
via public members (e.g. foo.x, foo.y, foo.z).

Is this possible? (will post ideas in a second)


Yes, it is possible.

V
Jul 23 '05 #3
jimmy wrote:
public:
// add some compiler directive to tightly pack
float x;
float y;
float z;

// later on use memcpy(&foo.x, p_float_data, 3*sizeof(float))

Would this work? How do I tell the compiler to tightly pack? would the
memcpy cause a runtime error?


You could simply declare a real array, and then make x, y, and z the
references to the array elements.

V
Jul 23 '05 #4
Victor Bazarov wrote:
jimmy wrote:
I would like to initialize a member array of 3 floats with a memcpy
(for reasons of efficiency).

Whatever your reasons, they are probably important to you.


And yet, if the OP doesn't know how to initialize the array, I doubt
fully they understand the the need for the 'efficiency'

Later I would like to access the floats
via public members (e.g. foo.x, foo.y, foo.z).

Is this possible? (will post ideas in a second)

Yes, it is possible.


;-)

V

Jul 23 '05 #5
Victor Bazarov wrote:
jimmy wrote:
public:
// add some compiler directive to tightly pack
float x;
float y;
float z;

// later on use memcpy(&foo.x, p_float_data, 3*sizeof(float))

Would this work? How do I tell the compiler to tightly pack? would the
memcpy cause a runtime error?


You could simply declare a real array, and then make x, y, and z the
references to the array elements.


Yes, but that's likely to increase the size of the class considerably (I'd
expect a factor of 2 to 3).

Jul 23 '05 #6
Rolf Magnus wrote:
Victor Bazarov wrote:

jimmy wrote:
public:
// add some compiler directive to tightly pack
float x;
float y;
float z;

// later on use memcpy(&foo.x, p_float_data, 3*sizeof(float))

Would this work? How do I tell the compiler to tightly pack? would the
memcpy cause a runtime error?


You could simply declare a real array, and then make x, y, and z the
references to the array elements.

Yes, but that's likely to increase the size of the class considerably (I'd
expect a factor of 2 to 3).


Why? References do not necessarily take up space, as you may know.

V
Jul 23 '05 #7
jimmy wrote:
I would like to initialize a member array of 3 floats with a memcpy
Don't be silly.
for reasons of efficiency).

(Later I would like to access the floats
via public members (e.g. foo.x, foo.y, foo.z).
That's a bad idea.
Is this possible? (will post ideas in a second)
Just do this:
cat foo.cc

class foo {
private:
// representation
float a[3];
public:
// functions
const
float& x(void) const { return a[0]; }
float& x(void) { return a[0]; }
const
float& y(void) const { return a[1]; }
float& y(void) { return a[1]; }
const
float& z(void) const { return a[2]; }
float& z(void) { return a[2]; }
// constructors
foo(void) {
x() = 0.0;
y() = 0.0;
z() = 0.0;
}
};
Jul 23 '05 #8
Victor Bazarov wrote:
Rolf Magnus wrote:
Victor Bazarov wrote:

jimmy wrote:

public:
// add some compiler directive to tightly pack
float x;
float y;
float z;

// later on use memcpy(&foo.x, p_float_data, 3*sizeof(float))

Would this work? How do I tell the compiler to tightly pack? would the
memcpy cause a runtime error?

You could simply declare a real array, and then make x, y, and z the
references to the array elements.

Yes, but that's likely to increase the size of the class considerably
(I'd expect a factor of 2 to 3).


Why? References do not necessarily take up space, as you may know.


They usually do if used as members of classes. A reference doesn't
necessarily refer to a member variable of the same object. It could also
refer to another variable, and how would the program know which one if its
address isn't stored somewhere?
I know this is compiler specific (which is why I added the word "likely"),
but I don't think you can find a compiler that doesn't do it like that.

Jul 23 '05 #9
As the example demonstrates, I am trying to load a 3D model from disk
(i.e. need to load lots of data fast).

I assumed a memcpy of 3 doubles would be faster than 3 assignments.

I also need to do manipulate the coordinates alot. I assumed read/write
would be faster via public access (not to mention backwards compatible
with the existing C implementation that uses structs).

They perhaps are false assumptions but seem reasonable to me. -j

Jul 23 '05 #10
In message <d6**********@nntp1.jpl.nasa.gov>, E. Robert Tisdale
<E.**************@jpl.nasa.gov> writes
jimmy wrote:
I would like to initialize a member array of 3 floats with a memcpy
Don't be silly.


I don't like using memcpy, but it's a sensible enough way to get data
into an array of built-in type.
for reasons of efficiency).
(Later I would like to access the floats
via public members (e.g. foo.x, foo.y, foo.z).
That's a bad idea.


Inviting a proposed solution which doesn't share the reasons why it's
bad.
Is this possible? (will post ideas in a second)
Just do this:
> cat foo.cc

class foo {
private:
// representation
float a[3];
public:
// functions
const
float& x(void) const { return a[0]; }


Const function returning non-const reference to member? Are you sure?
float& x(void) { return a[0]; }
Did you mean
float & x();
float x() const;

or maybe
float & x();
float const & x() const;
?

Either way, what have you gained? As soon as you have access functions
returning references, you've exposed the implementation, and might just
as well have made the member data public in the first place.
const
float& y(void) const { return a[1]; }
float& y(void) { return a[1]; }
const
float& z(void) const { return a[2]; }
float& z(void) { return a[2]; }
// constructors
foo(void) {
x() = 0.0;
y() = 0.0;
z() = 0.0;
}
Ugh.
};


--
Richard Herring
Jul 23 '05 #11
jimmy wrote:
As the example demonstrates, I am trying to load a 3D model from disk
(i.e. need to load lots of data fast).

I assumed a memcpy of 3 doubles would be faster than 3 assignments.
If you need maximum speed, assumptions are not a good thing. Btw: Some
compilers actually do automatically make use of memcpy in such a situation.
On others, memcpy might be slower than the assignments. You should measure
the speed or at least look at the generated assembler code to see what the
compiler made out of it. More often than not, you might be surprised.
I also need to do manipulate the coordinates alot. I assumed read/write
would be faster via public access (not to mention backwards compatible
with the existing C implementation that uses structs).


An inline function that returns a reference ususally gets optimized away
completely. And if you need it to be POD, you can still keep the array
member public.

Jul 23 '05 #12
Richard Herring wrote:
Just do this:
> cat foo.cc

class foo {
private:
// representation
float a[3];
public:
// functions
const
float& x(void) const { return a[0]; }


Const function returning non-const reference to member? Are you sure?


Where?
float& x(void) { return a[0]; }


Did you mean
float & x();
float x() const;

or maybe
float & x();
float const & x() const;
?

Either way, what have you gained? As soon as you have access functions
returning references, you've exposed the implementation, and might just
as well have made the member data public in the first place.


The difference is that the member is still an array, but when accessing it,
you don't have to write myvec.a[2], but rather myvec.z(), which shows the
intent much more clearly.

Jul 23 '05 #13
Richard Herring wrote:
In message <d6**********@nntp1.jpl.nasa.gov>, E. Robert Tisdale
<E.**************@jpl.nasa.gov> writes

[snip]
Just do this:
> cat foo.cc

class foo {
private:
// representation
float a[3];
public:
// functions
const
float& x(void) const { return a[0]; }


Const function returning non-const reference to member? Are you sure?
float& x(void) { return a[0]; }


Did you mean
float & x();
float x() const;

or maybe
float & x();
float const & x() const;
?

Either way, what have you gained? As soon as you have access functions
returning references, you've exposed the implementation, and might just
as well have made the member data public in the first place.


Well, it can be an advantage to have x, y, and z as an array float[3] in
your class. Think of vector arithmetic, think of memcopy, memmove. The
array makes sure that x, y, and z are alligned properly and can be
addressed by member-functions through an index (could be good for computing
cross-products and inner produtcs). In fact, it might be even better to use
boost::array. These are aspects of the implementation that are *not*
exposed by the proposed interface.
Best

Kai-Uwe Bux
Jul 23 '05 #14
In message <d6*************@news.t-online.com>, Rolf Magnus
<ra******@t-online.de> writes
Richard Herring wrote:
Just do this:

> cat foo.cc
class foo {
private:
// representation
float a[3];
public:
// functions
const
float& x(void) const { return a[0]; }


Const function returning non-const reference to member? Are you sure?


Where?


Right there:
float& x(void) const { return a[0]; }


A constant function taking no arguments and returning a non-const
reference to a float, namely a[0].

--
Richard Herring
Jul 23 '05 #15
Richard Herring wrote:
In message <d6*************@news.t-online.com>, Rolf Magnus
<ra******@t-online.de> writes
Richard Herring wrote:
Just do this:

> cat foo.cc
class foo {
private:
// representation
float a[3];
public:
// functions
const
float& x(void) const { return a[0]; }
Const function returning non-const reference to member? Are you sure?

Where?

Right there:
float& x(void) const { return a[0]; }

A constant function taking no arguments and returning a non-const
reference to a float, namely a[0].


I guess you're not so good at reading declarations that span two lines,
are you? Just look at the line _preceding_ the one you're referring to.

V
Jul 23 '05 #16
In message <d6*************@news.t-online.com>, Rolf Magnus
<ra******@t-online.de> writes
Richard Herring wrote:
Just do this:

> cat foo.cc
class foo {
private:
// representation
float a[3];
public:
// functions
const
float& x(void) const { return a[0]; }


Const function returning non-const reference to member? Are you sure?


Where?
float& x(void) { return a[0]; }


Did you mean
float & x();
float x() const;

or maybe
float & x();
float const & x() const;
?

Either way, what have you gained? As soon as you have access functions
returning references, you've exposed the implementation, and might just
as well have made the member data public in the first place.


The difference is that the member is still an array, but when accessing it,
you don't have to write myvec.a[2], but rather myvec.z(), which shows the
intent much more clearly.

True.

But in both cases the implementation is exposed, you're committed to
maintaining some kind of member which can be returned as a reference,
and you lose the opportunity to perform contract-enforcement checks on
any value that gets assigned to it.

--
Richard Herring
Jul 23 '05 #17
In message <d6**********@murdoch.acc.Virginia.EDU>, Kai-Uwe Bux
<jk********@gmx.net> writes
Richard Herring wrote:
In message <d6**********@nntp1.jpl.nasa.gov>, E. Robert Tisdale
<E.**************@jpl.nasa.gov> writes[snip]
Just do this:

> cat foo.cc
class foo {
private:
// representation
float a[3];
public:
// functions
const
float& x(void) const { return a[0]; }


Const function returning non-const reference to member? Are you sure?
float& x(void) { return a[0]; }


Did you mean
float & x();
float x() const;

or maybe
float & x();
float const & x() const;
?

Either way, what have you gained? As soon as you have access functions
returning references, you've exposed the implementation, and might just
as well have made the member data public in the first place.


Well, it can be an advantage to have x, y, and z as an array float[3] in
your class.


Certainly.
Think of vector arithmetic, think of memcopy, memmove.
I did. After all, hat's what the original question was about.
The
array makes sure that x, y, and z are alligned properly and can be
addressed by member-functions through an index (could be good for computing
cross-products and inner produtcs). In fact, it might be even better to use
boost::array. These are aspects of the implementation that are *not*
exposed by the proposed interface.


Indeed. But if you make the "setter" function take an argument instead
of returning a reference, there's no need to expose _anything_.

--
Richard Herring
Jul 23 '05 #18
Richard Herring wrote:
Const function returning non-const reference to member? Are you sure?


Where?


Right there:
const
float& x(void) const { return a[0]; }


Still can't see it ;-)

Jul 23 '05 #19
Richard Herring wrote:
Either way, what have you gained? As soon as you have access functions
returning references, you've exposed the implementation, and might just
as well have made the member data public in the first place.


The difference is that the member is still an array, but when accessing
it, you don't have to write myvec.a[2], but rather myvec.z(), which shows
the intent much more clearly.

True.

But in both cases the implementation is exposed, you're committed to
maintaining some kind of member which can be returned as a reference,
and you lose the opportunity to perform contract-enforcement checks on
any value that gets assigned to it.


That's right. However, I'm just viewing this from another perspective, since
I have done similar things, too, for realtime 3D graphics using OpenGL. In
my case, the class needed to be as efficient as possible, since there will
be millions of instances of it, and every second, millions of operations on
instances of it need to be executed. In addition to that, OpenGL is a C API
that wants pointers to raw arrays of float, so the data needs to be exposed
and needs to be in the form of an array.

Jul 23 '05 #20
In message <yv*******************@newsread1.mlpsca01.us.to.ve rio.net>,
Victor Bazarov <v.********@comAcast.net> writes
Richard Herring wrote:
In message <d6*************@news.t-online.com>, Rolf Magnus
<ra******@t-online.de> writes
Richard Herring wrote:

> Just do this:
>
> > cat foo.cc
> class foo {
> private:
> // representation
> float a[3];
> public:
> // functions
> const
> float& x(void) const { return a[0]; }
Const function returning non-const reference to member? Are you sure?
Where? Right there:
> float& x(void) const { return a[0]; }

A constant function taking no arguments and returning a non-const
reference to a float, namely a[0].


I guess you're not so good at reading declarations that span two lines,
are you?


Only when I write them ;-)

It's been a long day. I take that point back, and substitute a
different one. Who on earth splits that kind of declaration over two
lines, unless they deliberately intend to cause confusion?
Just look at the line _preceding_ the one you're referring to.


But my comment about the following line still stands.

--
Richard Herring
Jul 23 '05 #21
I was infact using "getters" with my initial implementation. I came to
the same realization as Richard; that by using getters I might as well
just make them public.

The implementations are about equal length because I needed to add a
copy cons. and assign. op. for the implementation using reference
members.

I am fairly convinced that using assignment and getters can be only "as
good" as using memcpy and public access. Though as Rolf has pointed
out, the only way to surely tell is to profile and/or look at the
assembly.

Thanks everyone for your help and opinions. -J

"And yet, if the OP doesn't know how to initialize the array, I doubt
fully they understand the the need for the 'efficiency'" -Andrew

I am pretty sure I understand the need for efficiency. I am just not a
seasoned c++ programmer. That's why I am asking for help.

Jul 23 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
6
by: blueblueblue2005 | last post by:
here is a friend function of Class Array, which has two private data member: int size, int *ptr // Array's public member function to return size int getSize() const { return size; } friend...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
9
by: Haobin | last post by:
Hi everyone, I have an ArrayList whose members are classes. These classes are derived from a same base class. The base class has a floating point array and a string. How do I access each element in...
3
by: Chua Wen Ching | last post by:
Hi there, I had seen examples for classes, but i had no idea how to implement the same thing in struct. I am quite mix up! Which one is correct? Scenario: WForm.cs - the one that calls...
5
by: sherifffruitfly | last post by:
Hi, I'm just learning cpp, and the exercise I'm working on is basically as follows: 1) Create a struct type with 4 members (char, char, char, int). 2) Create an array of, say 3 instances of...
6
by: noone | last post by:
What is the syntax to access members of a structure without explicitly naming the structure in every access? struct mytype { int a; char* b; long c; } IT; How can I access the structure...
10
by: blangela | last post by:
If I pass a base class object by reference (likely does not make a difference here that it is passed by reference) as a parameter to a derived class member function, the member function is not...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.