473,395 Members | 1,658 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,395 software developers and data experts.

Large private data member access

Hi all,
I guess this is more of a design problem than a language problem, but
I'm confused either way! I have a class and it has a private data member
which is a struct. The size of the struct is what I would call relatively
large (about 1Mb). I have written methods for this class so that the struct
can be correctly filled with the corerct data and certain parts of the
struct can be extracted. But the problem I face is that I now have another
class that will also hold a private data member of the same struct, but this
class will work on the struct in a very different way. The thing I can't
resolve is that I want to transfer the contents of the struct from one class
to the other ... these are my ideas of how to do it but I don't know which
is best to use!

1. I could have a 'GetStruct' method which would return a const reference to
my private struct and then have my second class use memcpy to fill its
struct. For some reason I feel nervous giving out any reference to a private
member as it doesn't take much to defeat the 'constness' and mess around
with the internal struct ... I've had someone do that before with my code
before!

2. I could write a helper class that behaves rather like an STL iterator. I
could then get 'iterators' to the begining and end of my struct and transfer
it byte by byte. If possible I want to avoid using the STL itself because
this code may well end up in an embedded system and my customer isn't keen
on STL (despite my protesting!).

I'm sure this is a fairly standard problem that people come across all the
time, can anyone suggest the prefered/best method for doing this - oh and I
want to try and keep things as quick as possible.

Thanks,
Rich.
Jul 23 '05 #1
3 2040
Richard Webb wrote:
....

I'm sure this is a fairly standard problem that people come across all the
time, can anyone suggest the prefered/best method for doing this - oh and I
want to try and keep things as quick as possible.


Without knowing more about your issue, it's hard to give you valuable
advice. However a few shots in the dark:

a) Use friend declarations. This way one of your classes is able to
access the other's private parts.

b) If you're trying to avoid copying the data, you may use a reference
counted wrapper class that is private and make the other two classes
friends.
Jul 23 '05 #2

"Richard Webb" <we***@REMOVEMErichardwebb-online.co.uk> wrote in message
news:Y6********************@pipex.net...
Hi all,
I guess this is more of a design problem than a language problem, but
I'm confused either way! I have a class and it has a private data member
which is a struct. The size of the struct is what I would call relatively
large (about 1Mb). I have written methods for this class so that the
struct
can be correctly filled with the corerct data and certain parts of the
struct can be extracted. But the problem I face is that I now have another
class that will also hold a private data member of the same struct, but
this
class will work on the struct in a very different way. The thing I can't
resolve is that I want to transfer the contents of the struct from one
class
to the other ... these are my ideas of how to do it but I don't know which
is best to use!

1. I could have a 'GetStruct' method which would return a const reference
to
my private struct and then have my second class use memcpy to fill its
struct. For some reason I feel nervous giving out any reference to a
private
member as it doesn't take much to defeat the 'constness' and mess around
with the internal struct ... I've had someone do that before with my code
before!

2. I could write a helper class that behaves rather like an STL iterator.
I
could then get 'iterators' to the begining and end of my struct and
transfer
it byte by byte. If possible I want to avoid using the STL itself because
this code may well end up in an embedded system and my customer isn't keen
on STL (despite my protesting!).

I'm sure this is a fairly standard problem that people come across all the
time, can anyone suggest the prefered/best method for doing this - oh and
I
want to try and keep things as quick as possible.


I'm not sure I understand the problem entirely. Why are you thinking of
doing all that byte-by-byte copying? If you need to copy the data, why not
use an assignment operator for it? (Or the default assignment operator, if
the internal struct is a POD type.)

Do you actually need a copy of that internal data structure, or do you just
need to get information from it that you use to compute other information?
If you don't actually *need* another copy of all that data, then you could
simply refer to the first object's struct via any method you wish, such as a
const reference (as you mentioned). Or, perhaps better, getter functions
that return just the parts you need from that private structure. Or even
member functions of that first object that do whatever operations the second
object needs done to the internal struct, assuming it does both "gets" and
"sets".

Without more info, it's hard to guess what would work best for your case. (I
think what confused me most was your statement that the second object holds,
in your words, a private data member "of the same struct". I can't tell if
you meant just "the same struct type" or an "exact copy", or a "copy, but
one which will then be changed so that it's different".

-Howard

Jul 23 '05 #3
> I'm not sure I understand the problem entirely. Why are you thinking of
doing all that byte-by-byte copying? If you need to copy the data, why not use an assignment operator for it? (Or the default assignment operator, if the internal struct is a POD type.)

Do you actually need a copy of that internal data structure, or do you just need to get information from it that you use to compute other information?
If you don't actually *need* another copy of all that data, then you could
simply refer to the first object's struct via any method you wish, such as a const reference (as you mentioned). Or, perhaps better, getter functions
that return just the parts you need from that private structure. Or even
member functions of that first object that do whatever operations the second object needs done to the internal struct, assuming it does both "gets" and
"sets".

Without more info, it's hard to guess what would work best for your case. (I think what confused me most was your statement that the second object holds, in your words, a private data member "of the same struct". I can't tell if you meant just "the same struct type" or an "exact copy", or a "copy, but
one which will then be changed so that it's different".

-Howard


Howard,
Thanks for your reply. I already have functions that extract specific
pieces of information from the struct. But the problem is that I want
another class to have access to this entire large member object. The simple
straight answer is to provide what I'm doing already and that is to return a
const reference to the struct which is a private member. I was simply trying
to have another class access the entire struct, ideally without copying it.
The only reason I'm trying to think of a new method of doing this is that I
have had occassion before where the other people who've used my classes have
not been the best C++ coders in the world and have defeated the constness of
a const reference to a private member ... then accessed the private member
and broken the operation of the class. I'm trying to add an extra layer of
fool-proofness that doesn't impose a large overhead by having to copy the
data.

I think though that I may be wasting my time a little bit with this idea. I
will stick with the answer that I normally give which is if you use my class
incorrectly that's your fault!!

Thanks,
Rich.
Jul 23 '05 #4

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

Similar topics

13
by: Chris Smith | last post by:
'Morning, Within the next few months, I'm going to embark upon a comparatively rather large base of JavaScript code to be called from a web browser environment. Not too awfully huge, but...
34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
3
by: quo | last post by:
two questions: 1) Does this program demonstrate the basic difference between public and private access? It appears correct to say that instances of a class cannot directly call a private...
12
by: Manolis | last post by:
Hi, I was wondering if there is any way to make two objects of the same class to be able to access each other's private data, like this: class A { public: void access( const A& a )...
8
by: Darsant | last post by:
I'm currently reading 1-n number of binary files, each with 3 different arrays of floats containing about 10,000 values a piece for a total of about 30,000 values per file. I'm looking for a way...
8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
2
by: Christoph Boget | last post by:
Let's take the following class: class MyClass { private int privateVar; public int PublicVar { get { return privateVar; } } public MyClass() {}
6
by: Ajay Martin | last post by:
Why would it be reasonable for someone to argue that it is incorrect to allow a public member inherited from a public base class to be redefined as private?
8
by: Ethan Kennerly | last post by:
Hello, There are a lot of Python mailing lists. I hope this is an appropriate one for a question on properties. I am relatively inexperienced user of Python. I came to it to prototype...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.