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 21 1626
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
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
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
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
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).
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
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;
}
};
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.
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
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
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.
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.
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
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
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
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
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
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 ;-)
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.
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |