c++ - Sharing object by reference or pointer -
Say I have a type of A that contains the initialize () method. The method receives object B, which is kept as an object data member. The B object is shared between many objects, thus A must have the originally received B object and not its copy.
class A {public: bool initialize (b? B); Private: B ?? M_B; }
object B should be present. Thus, I feel that in order to pass it by reference, instead of going through the indicator, and in the case of failure initially, B is in the tap.
The only type of orderer for the MB is the type of B (it can not be referenced to B because the intializating of B is not done in AC-torb). Here's how to get started:
bool A :: start (B & B) {m_B = & amp; Amp; B; ....}
Is this aproach okay?
UPD:
-
The code is not new and I'm just trying to "fix" some problems. Indeed, I am not talking about some concrete A and B classes, rather than solving the problem in my code base, the code extends widely to B at the pointers and verifies it in initialize if it Tap is there.
-
Passing A's A-c-wire from B is not always a good option. A also has other parameters, which are not present in A creation time. So I do not like to pass AC-Tor part of the parameters and the rest in A: Initiative (). The shared APPR can also be "null", so to pass it, the A: Initiative (B) is not different from just giving the indicator, in that aspect Initial signature () has not declared if B is mandatory or not. In my case it is and I want to express it in terms of B.
-
Our code is not currently using the promotion of everyone. So, instead of just passing the raw pointers, rather than the better solution shared, the solution suggested by me can be considered bad, but still the solution.
I'll be with the pointer reference here only sends the wrong message.
When you take an object object If you share or share it, you do not use the context of objects in the circumstances, etc. The main reason for reference in C ++, copy the constructor to work for operator overloading and user defined types. This functionality is available It will not be difficult to do so, which is not different in synthetic formation.
But in this situation you are not trying to copy the type created in. You are working on the object, Is used through the medium and even shared through many different pointers. So be clear about that .
As a result of being a b
, emphasize in all ways )
(or similar construction) to implement the contract and prevent invalid programs. (I will not spare the exception even though. If you forget about the problems of exceptions in C ++, can you ever Are you planning to capture and handle such exceptions?) I will also add code that uses m_B
if anyone has A :: Initialize ()
.
If you have forgotten to make a call, then use the reference to make sure that the indicator is not zero, in most implementations, you can refer to the tap or the hanging indicator without raising any errors. . Your application will fail only when you try to use this reference. So if someone accidentally passes you through B * pb
, you can call pa-> Initialize (* pb)
and nothing else except that pa-> M_B
is now NULL.
Using something like boost :: shared_ptr
is yours and your memory strategy is management. This is a completely unrelated issue.
Comments
Post a Comment