home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19808 < prev    next >
Encoding:
Text File  |  1993-01-23  |  4.1 KB  |  141 lines

  1. Path: sparky!uunet!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!ira.uka.de!math.fu-berlin.de!informatik.tu-muenchen.de!rz.uni-passau.de!hiwi170.rz.uni-passau.de!schmidt
  2. From: schmidt@rz.uni-passau.de (SCHMIDT GUIDO)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Question of scope
  5. Date: Sat, 23 Jan 1993 14:58:05 GMT
  6. Organization: University of Passau - Germany
  7. Lines: 129
  8. Message-ID: <schmidt.33@rz.uni-passau.de>
  9. References: <1993Jan22.184558.21397@porthos.cc.bellcore.com>
  10. NNTP-Posting-Host: hiwi170.rz.uni-passau.de
  11.  
  12. In article <1993Jan22.184558.21397@porthos.cc.bellcore.com> chw@bellcore.com (Charlie Woloszynski,MRE 2J-278,8295228,,27143) writes:
  13.  
  14.  
  15. >A (novice) question about scope.
  16. >I have a class Base from which I derive several additional classes, as in:
  17. >class Base 
  18. >{ 
  19. >  virtual Draw(); 
  20. >} 
  21. >class Derived1 : public Base 
  22. >{ 
  23. >  virtual Draw(); 
  24. >} 
  25. >I need to pass a pointer to an instance of one of these derived classes 
  26. >to a function (so I can make use of the derived functions) like:
  27. >void foo(Base *p) 
  28. >{ 
  29. >  p->Draw(); 
  30. >} 
  31. >Now I generally need to create the Derived classes only to pass them
  32. >to foo.  I don't want these instances to exist after the call to foo.
  33. >Anyway, does the following call the destructor just after returning
  34. >from the foo?
  35. >   foo(& Derived1());
  36. >Does it have any ugly portability problems? 
  37. >Charlie Woloszynski
  38. >chw@bellcore.com
  39.  
  40. Hello!
  41.  
  42. I've tried this in the following short program. It seems to work. At the end
  43. of main() there are two different ways stated to do this. I don't know which
  44. one is better. Any comments from others please.
  45.  
  46. // polytest.cpp
  47. #include <iostream.h>
  48.  
  49. class    Point {
  50.     public:
  51.         Point( int xx = 0, int yy = 0 ) : x(xx), y(yy) { cout << "Point()" << endl; }
  52.         Point( const Point& p ) : x(p.x), y(p.y) { cout << "Point(XX&)" << endl; }
  53.         virtual ~Point() { cout << "~Point()" << endl; }
  54.         virtual void Show() { cout << "Point.Show()" << endl; }
  55.         virtual void Hide() { cout << "Point.Hide()" << endl; }
  56.         void MoveTo( int xx, int yy ) {
  57.             Hide();         // the correct Hide() method will be established at runtime
  58.             x = xx; y = yy; // this is the same for every class
  59.             Show();         // the correct Show() method will be established at runtime
  60.         }
  61.     protected:
  62.         int x;
  63.         int y;
  64. };
  65. class Circle : public Point {
  66.     public:
  67.         Circle( int xx = 0, int yy = 0, int rr = 0 ) : Point(xx, yy), r(rr) { cout << "Circle()" << endl; }
  68.         Circle( const Circle& c ) : Point(c.x, c.y), r(c.r) { cout << "Circle(XX&)" << endl; }
  69.         virtual ~Circle() { cout << "~Circle()" << endl; }
  70.         virtual void Show() { cout << "Circle.Show()" << endl; }
  71.         virtual void Hide() { cout << "Circle.Hide()" << endl; }
  72.     protected:
  73.         int r;  // radius
  74. };
  75. class Rectangle : public Point {
  76.     public:
  77.         Rectangle( int xx = 0, int yy = 0, int ww = 0, int hh = 0 ) : Point(xx, yy), w(ww), h(hh) { cout << "Rectangle()" << endl; }
  78.         Rectangle( const Rectangle& r ) : Point(r.x, r.y), w(r.w), h(r.h) { cout << "Rectangle(XX&)" << endl; }
  79.         virtual ~Rectangle() { cout << "~Rectangle()" << endl; }
  80.         virtual void Show() { cout << "Rectangle.Show()" << endl; }
  81.         virtual void Hide() { cout << "Rectangle.Hide()" << endl; }
  82.     protected:
  83.         int w;  // width
  84.         int h;  // height
  85. };
  86. void showfunction( Point * p ) {
  87.     p->Show();
  88. }
  89. int main() {
  90.     Point * X;
  91.     X = new Point(10, 10);
  92.     X->Show();
  93.     X->MoveTo(20, 20);
  94.     X->Hide();
  95.     delete X;
  96.     X = new Circle(10, 10, 100);
  97.     X->Show();
  98.     X->MoveTo(20, 20);
  99.     X->Hide();
  100.     delete X;
  101.     X = new Rectangle(10, 10, 100, 200);
  102.     X->Show();
  103.     X->MoveTo(20, 20);
  104.     X->Hide();
  105.     delete X;
  106.  
  107. // !!!  N E W !!!
  108.     cout << endl << "New:" << endl;
  109.  
  110.     showfunction( &Rectangle(10,10,100,200) );
  111.     cout << "This seems to work." << endl;
  112.  
  113.     X = new Rectangle(10, 10, 100, 200);
  114.     showfunction(X);
  115.     delete X;
  116.     cout << "This also seems to work." << endl;
  117.  
  118.     return 0;
  119. }
  120. // eof
  121.  
  122. Guido.
  123. --------------------------------------------------------------------
  124. - Guido Schmidt                                                    -
  125. - schmidt@rz.uni-passau.de                                         -
  126. - Universitaet Passau, Germany                                     -
  127. --------------------------------------------------------------------
  128.  
  129.