About pointers and private membership

J

joshipura

I have a basic question.
I want to communicate a value v [2D array of characters] between two objects o1 and o2 of classes c1 and c2 respectively. Something like this:

c1's code calls
o2::method; //should return value v

My question is, if this value is defined as a private member of o2/c2, should not this address be invalid/illegal in the scope of o1?

Then, if I define the array as a member of o1/c1 and pass it by reference, should it not be invalid/illegal in the scope of o2?

If private members don't have any meaning outside the object, how do pointers ever pass?
 
V

Victor Bazarov

I have a basic question.
I want to communicate a value v [2D array of characters] between two
objects o1 and o2 of classes c1 and c2 respectively. Something like this:
c1's code calls
o2::method; //should return value v

My question is, if this value is defined as a private member of
o2/c2, should not this address be invalid/illegal in the scope of
o1?

Address space is by process (or thread), not by object. If your object
decides to share the address of its member with any other object, that's
perfectly OK, and the object that gets the address shall be able to see
the contents (provided the pointer still points to valid memory at the
time of access, and hasn't been deallocated).
Then, if I define the array as a member of o1/c1 and pass it by
reference, should it not be invalid/illegal in the scope of o2?

It should be OK, as long as the memory is intact and the o1/c1 is intact.
If private members don't have any meaning outside the object, how do
pointers ever pass?

Exactly.

If you have a problem, consider stating the problem (see FAQ 5.8 for
more information). If you don't have a problem (yet), consider coding
instead of asking about it.

V
 
J

joshipura

Such

practice however kind of defeats the meaning of private, so one must be

careful.

Exactly.
I started programming in C++ and needed to pass around bigger data. The dilemma I am having is this killing the meaning of "private"!
If I pass around pointers (from caller to called or vice-verse) it feels unclean - else, I don't know how to get the task accomplished.

The more specific question is, what is a clean OO way to move a matrix from a view to a model?
 
V

Victor Bazarov

[..]
The more specific question is, what is a clean OO way to move a matrix from a view to a model?

If it's a transformation matrix, then by value should be OK. Make sure
your xfrm is a class that contains the actual matrix elements (and does
not manage dynamic memory instead).

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top