c++ - Class interaction tips -


I'm amazed by some issues while trying to design interactive classes.

Data from class B is either called as an indicator or standard container format, (or even more ambitious, and for STL containers as some shared APTR members) .), My method is to use a function that uses the same arguments and design class B method types according to the arguments? Is there a general rule of thumb to design interacting classes and share data between these classes? Can you please prepare a general plan for some common situations, which usually come into practice (and it is appreciated with some examples)? I think I should read some examples of class interaction in C ++, any signs on this are also appreciated?

There may be a small sample:

  #include & lt; Iostream & gt; # Include & lt; Vector & gt; #include & lt; Iterator & gt; # Include & lt; Cassert & gt; using namespace std; Class A {public: A (at s, int val): sj (s), val (val), v (new vector & lieutenant; at & g;) {} int fill_hector () {for (int q = 0 ; Kashmir!; + + K) v- & gt; Push_back (val); Return 0; } ~ A () {cout & lt; & Lt; "Dtor is removing the pointer to vector \ n"; Remove V; } Vector & lt; Int & gt; * Get_pointer_to_vector_in_A () {return V; } Private: int sz, val; Of vector & lt; Int & gt; * V; }; Class B {Public: B () {} // Nothing basically does anything print_vector_contents_from_A (vector  * v_from_A) const {(v_from_A-> empty ()); Copy (v_from_A- & gt; Start (), v_from_A- & gt; End (), ostream_iterator  (cout, "\ n")); }}; Int main () {A (10, 4); A.fill_vector (); BB; B.print_vector_contents_from_A (a.get_pointer_to_vector_in_A ()); Return 0; }   

It depends on the fact that A . If A can be validly viewed as a sequence of int , then I type size_t (const) and const and int & Amp; Operator [] (size_t) (+ its const equivalent) on that. These only make your activities v.size and v [] or v.at .

In b , you can define

  static zero B :: print_contents (a const & a) {for ( Size_t i = 0; i & lt; a.size (); I ++) std :: cout & lt; & Lt; A [i] & lt; & Lt; '\ N'; }   

Any member who has std :: vector & lt; Int & gt; * breaks: You can not modify the implementation of A std :: vector & lt; Int & gt; to get away from get_pointer_to_vector_in_A works with very unattractive hacks as well as the same words.

Comments

Popular posts from this blog

qt - switch/case statement in C++ with a QString type -

python - sqlite3.OperationalError: near "REFERENCES": syntax error - foreign key creating -

Python's equivalent for Ruby's define_method? -