A Copy of Pointer Is Dangerous?

Discussion in 'C++' started by Immortal_Nephi@hotmail.com, Sep 9, 2008.

Guest

Sometimes, unsigned char array is located in the file scope. You
define A Object and B Object. A Object and B Object need to share
unsigned char array. They are very different object. They are like
two chips.
A Object modifies data in the unsigned char array. Then, B Object is
in turn to modify data in the unsigned char array. Static data member
is not the answer. Unsigned char array in the file scope is a bad
idea because other functions or classes might modify unsigned char
array accidently. The only way is to guard it inside main() function.
You need to define two pointers. Two pointers are used to share
unsigned char array. You have to be careful when you write your code
in the correct sequence.
Sometimes, you may want to copy data member from A Object to B
Object. Friend class is the answer. Please say if Update() function
or Update2() function is better. Update2() function may have
additional overhead because "this pointer" reads memory address
indirection before indirection memory address is read again to another
indirection memory address and then accesses data member. Update()
function uses reference to A Object.
Compare Run() function in both A Object and B Object.
Place NULL in unsigned char array in both ~A() function and ~B()
function are unnecessary.
Please state your opinion what you think my good design C++ code.
Here is an example of my code below.

Advertisements

Guest

On Sep 8, 6:45 pm, wrote:
> A Object modifies data in the unsigned char array. Then, B Object is
> in turn to modify data in the unsigned char array. Static data member
> is not the answer. Unsigned char array in the file scope is a bad
> idea because other functions or classes might modify unsigned char
> array accidently. The only way is to guard it inside main() function.

Since introducing the shared data in main is simply natural, there is
no reason to put the data in file scope.
> Sometimes, you may want to copy data member from A Object to B
> Object. Friend class is the answer.

You can also use A:perator=. That way, you don't need to be a friend
of A.
> Please say if Update() function
> or Update2() function is better.

Could you have an A member in B, instead of having members of A? Then
this would be better:

Oh... Then they are probably equally expensive, because the reference
parameter is implemented as a pointer behind the scenes anyway.
> Compare Run() function in both A Object and B Object.

I see two functions that modify distinct parts of a third object.
> Place NULL in unsigned char array in both ~A() function and ~B()
> function are unnecessary.

Correct. Changing the members of a dead object shouldn't matter unless
you access that dead object after its death, which is undefined
behavior anyway.
> Please state your opinion what you think my good design C++ code.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!