home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13496 < prev    next >
Encoding:
Text File  |  1992-09-10  |  2.7 KB  |  195 lines

  1. Path: sparky!uunet!mcsun!uknet!keele!nott-cs!lut.ac.uk!cojm2
  2. From: J.March@lut.ac.uk
  3. Newsgroups: comp.lang.c++
  4. Subject: Arguement passing, variables and constructors
  5. Message-ID: <1992Sep10.174606.23374@lut.ac.uk>
  6. Date: 10 Sep 92 17:46:06 GMT
  7. References: <etxolpn.716117347@beppe> <1992Sep10.120145.25280@lut.ac.uk>
  8. Reply-To: J.March@lut.ac.uk (Jon March)
  9. Organization: Loughborough University, UK.
  10. Lines: 183
  11.  
  12. I want to implement some container libraries.  They must be efficient but I'm
  13. unsure about the way in which constructors, copy-constructors, assignment and
  14. other arguement passing issues effect execution speed.  I would appreciate a
  15. blow by blow answer.  I do realise that inlineing would should also be used in
  16. optimisation but I didn't want to clutter this question...
  17.  
  18.  
  19. What happens to the arguements and variables during the execution of the 
  20. examples below?
  21.  
  22. Which is most efficient (a hybrid could be suggested)?
  23.  
  24. Any other comments...
  25.  
  26.  
  27. Please reply to this in the group (but first check to see if anyone else has 
  28. beaten you to it :-( ) as I think that a few others would like to know the 
  29. answer to this,
  30.  
  31. Jon
  32.  
  33.  
  34.  
  35. /*--------------------------- cut here -------------------------------------*/
  36. // eg 1
  37.  
  38.  
  39. class A
  40. {
  41.     int i;
  42. public:
  43.     A() : i(0) {}
  44. };
  45.  
  46.  
  47. class T
  48. {
  49.     A*    p;
  50.     T*    next;
  51. public:
  52.         T() : p(0), next(0) {}
  53.         T(const A);
  54.         T(const A, const T);
  55.     T    f(const A) const;
  56. };
  57.  
  58.  
  59. T::T(const A q)
  60. {
  61.     p = new A(q);
  62.     next = new T;
  63. }
  64.  
  65.  
  66. T::T(const A q, const T t)
  67. {
  68.     p = new A(q);
  69.     next = new T(t);
  70. }
  71.  
  72.  
  73. T T::f(const A q) const
  74. {
  75.     return T(q, *this);
  76. }
  77.  
  78.  
  79. void main()
  80. {
  81.     T t1;
  82.     A a1;
  83.  
  84.     T t2 = t1.f(a1);
  85. }
  86.  
  87.  
  88.  
  89.  
  90. /*--------------------------- cut here -------------------------------------*/
  91. // eg 2
  92.  
  93.  
  94. class A
  95. {
  96.     int i;
  97. public:
  98.     A() : i(0) {}
  99. };
  100.  
  101.  
  102. class T
  103. {
  104.     A*    p;
  105.     T*    next;
  106. public:
  107.         T() : p(0), next(0) {}
  108.         T(const A&);
  109.         T(const A&, const T&);
  110.     T    f(const A&) const;
  111. };
  112.  
  113.  
  114. T::T(const A& q)
  115. {
  116.     p = new A(q);
  117.     next = new T;
  118. }
  119.  
  120.  
  121. T::T(const A& q, const T& t)
  122. {
  123.     p = new A(q);
  124.     next = new T(t);
  125. }
  126.  
  127.  
  128. T T::f(const A& q) const
  129. {
  130.     return T(q, *this);
  131. }
  132.  
  133.  
  134. void main()
  135. {
  136.     T t1;
  137.     A a1;
  138.  
  139.     T t2 = t1.f(a1);
  140. }
  141.  
  142.  
  143.  
  144.  
  145. /*--------------------------- cut here -------------------------------------*/
  146. // eg 3 
  147.  
  148.  
  149. class A
  150. {
  151.     int i;
  152. public:
  153.     A() : i(0) {}
  154. };
  155.  
  156.  
  157. class T
  158. {
  159.     A*    p;
  160.     T*    next;
  161. public:
  162.         T() : p(0), next(0) {}
  163.         T(A*);            // compiler:
  164.         T(A*, T*);        // no standard conversion of  
  165. friend    T*    f(A*, T*);         //      const T * to  T *
  166. };
  167.  
  168. T::T(A* q)                // I could have inlined this in the
  169. {                    // declaration but I didn't to keep
  170.     p = q;                // the examples consistant.
  171.     next = new T;
  172. }
  173.  
  174.  
  175. T::T(A* q, T* t)            // ditto
  176. {
  177.     p = q;
  178.     next = t;
  179. }
  180.  
  181.  
  182. T* f(A* q, T* t)
  183. {
  184.     return new T(q, t);
  185. }
  186.  
  187.  
  188. void main()
  189. {
  190.     T* t1 = new T;
  191.     A* a1 = new A;
  192.  
  193.     T* t2 = f(a1, t1);
  194. }
  195.