473,881 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2075
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***@REMOVEME richardwebb-online.co.uk> wrote in message
news:Y6******** ************@pi pex.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
1928
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 probably between 10K and 15K LoC. Having seen only the kinds of scripts that support interactive web pages, I'm dreading trying to stretch JavaScript that far. Has anyone here done something like this, and want to share some experience? In my...
34
3126
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 this in any way used to create singletons. Can someone say how? Cheers, Andy
3
2331
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 method, but a public method can be called by the instance to invoke the private method. 2) So is it true that only public methods of a class can invoke a private method of that same class?
12
2687
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 ) {cout<<"a.value="<<a.value<<endl; } private: int value;
8
2908
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 to load them all into memory. I've tried using vector pushback with reserving, but it was horribly slow. The current method I am using is upon opening the file and reading the number of values, resizing the vectors (I have 3, one for each data...
8
7835
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 scope they meant public/protected/private access modifiers :) Obviously all members of the base privately inherited class will be private, and that was my answer. However, the teacher checked my answers when I handed in, and the problem was that I had...
2
1833
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
1846
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
1650
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 concepts for videogames. Having programmed in C, scripted in Unix shells, and scripted in a number of proprietary game scripting languages, I'm impressed at how well Python meets my needs. In almost all respects, it does what I've been wishing a...
0
9926
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9776
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
11095
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10716
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...
0
10399
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...
1
7952
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3223
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.