473,847 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1690
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

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

Similar topics

8
3951
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 private members of nesting class. template <typename T> class Container { // NO friendship given to any other public: class ContainerIterator;
6
4120
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 istream &operator>>(istream &in, Array &a) { for(int i=0; i<a.size; i++) // do something
3
24041
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 tables in the database where the code resides. If we move the database with the data tables to a new directory, the links are no longer valid. I tried to update the links by changing the Connect property and refreshing: Set td = db.TableDefs(0)...
6
4770
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 appreciated. Thanks in advance
9
4055
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 the floating point array in a member of the ArrayList? Any help would be greatly appreciated. Haobin
3
1898
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 FileA.cs to access the structures FileA.cs - contains all structures
5
348
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 the struct, and populate them with data. 3) cin 1, 2, 3, or 4 from the user 4) If the user selected, say, 2, display the contents of the 2nd data
6
3028
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 members in a way similar to the old pascal
10
4124
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 allowed to access the protected data members of the base object. This surprises me. Can someone explain why this is? I suspect there is a good reason and I am just having a slow day to not come up with it myself. Bob
0
9727
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10645
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10706
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10331
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9480
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7052
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5718
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5909
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3160
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.