home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!uknet!keele!nott-cs!lut.ac.uk!cojm2
- From: J.March@lut.ac.uk
- Newsgroups: comp.lang.c++
- Subject: Arguement passing, variables and constructors
- Message-ID: <1992Sep10.174606.23374@lut.ac.uk>
- Date: 10 Sep 92 17:46:06 GMT
- References: <etxolpn.716117347@beppe> <1992Sep10.120145.25280@lut.ac.uk>
- Reply-To: J.March@lut.ac.uk (Jon March)
- Organization: Loughborough University, UK.
- Lines: 183
-
- I want to implement some container libraries. They must be efficient but I'm
- unsure about the way in which constructors, copy-constructors, assignment and
- other arguement passing issues effect execution speed. I would appreciate a
- blow by blow answer. I do realise that inlineing would should also be used in
- optimisation but I didn't want to clutter this question...
-
-
- What happens to the arguements and variables during the execution of the
- examples below?
-
- Which is most efficient (a hybrid could be suggested)?
-
- Any other comments...
-
-
- Please reply to this in the group (but first check to see if anyone else has
- beaten you to it :-( ) as I think that a few others would like to know the
- answer to this,
-
- Jon
-
-
-
- /*--------------------------- cut here -------------------------------------*/
- // eg 1
-
-
- class A
- {
- int i;
- public:
- A() : i(0) {}
- };
-
-
- class T
- {
- A* p;
- T* next;
- public:
- T() : p(0), next(0) {}
- T(const A);
- T(const A, const T);
- T f(const A) const;
- };
-
-
- T::T(const A q)
- {
- p = new A(q);
- next = new T;
- }
-
-
- T::T(const A q, const T t)
- {
- p = new A(q);
- next = new T(t);
- }
-
-
- T T::f(const A q) const
- {
- return T(q, *this);
- }
-
-
- void main()
- {
- T t1;
- A a1;
-
- T t2 = t1.f(a1);
- }
-
-
-
-
- /*--------------------------- cut here -------------------------------------*/
- // eg 2
-
-
- class A
- {
- int i;
- public:
- A() : i(0) {}
- };
-
-
- class T
- {
- A* p;
- T* next;
- public:
- T() : p(0), next(0) {}
- T(const A&);
- T(const A&, const T&);
- T f(const A&) const;
- };
-
-
- T::T(const A& q)
- {
- p = new A(q);
- next = new T;
- }
-
-
- T::T(const A& q, const T& t)
- {
- p = new A(q);
- next = new T(t);
- }
-
-
- T T::f(const A& q) const
- {
- return T(q, *this);
- }
-
-
- void main()
- {
- T t1;
- A a1;
-
- T t2 = t1.f(a1);
- }
-
-
-
-
- /*--------------------------- cut here -------------------------------------*/
- // eg 3
-
-
- class A
- {
- int i;
- public:
- A() : i(0) {}
- };
-
-
- class T
- {
- A* p;
- T* next;
- public:
- T() : p(0), next(0) {}
- T(A*); // compiler:
- T(A*, T*); // no standard conversion of
- friend T* f(A*, T*); // const T * to T *
- };
-
- T::T(A* q) // I could have inlined this in the
- { // declaration but I didn't to keep
- p = q; // the examples consistant.
- next = new T;
- }
-
-
- T::T(A* q, T* t) // ditto
- {
- p = q;
- next = t;
- }
-
-
- T* f(A* q, T* t)
- {
- return new T(q, t);
- }
-
-
- void main()
- {
- T* t1 = new T;
- A* a1 = new A;
-
- T* t2 = f(a1, t1);
- }
-