From: schmidt@rz.uni-passau.de (SCHMIDT GUIDO)
Newsgroups: comp.lang.c++
Subject: Re: Question of scope
Date: Sat, 23 Jan 1993 14:58:05 GMT
Organization: University of Passau - Germany
NNTP-Posting-Host: hiwi170.rz.uni-passau.de

In article <1993Jan22.184558.21397@porthos.cc.bellcore.com> chw@bellcore.com (Charlie Woloszynski,MRE 2J-278,8295228,,27143) writes:


>A (novice) question about scope.
> 
> 
>I have a class Base from which I derive several additional classes, as in:
> 
>class Base 
>{ 
>  virtual Draw(); 
>} 
> 
>class Derived1 : public Base 
>{ 
>  virtual Draw(); 
>} 
> 
>I need to pass a pointer to an instance of one of these derived classes 
>to a function (so I can make use of the derived functions) like:
> 
> 
>void foo(Base *p) 
>{ 
>  p->Draw(); 
>} 
> 
>Now I generally need to create the Derived classes only to pass them
>to foo.  I don't want these instances to exist after the call to foo.
> 
>Anyway, does the following call the destructor just after returning
>from the foo?
> 
>   foo(& Derived1());
> 
>Does it have any ugly portability problems? 
> 
>Charlie Woloszynski
>chw@bellcore.com

Hello!

I've tried this in the following short program. It seems to work. At the end
of main() there are two different ways stated to do this. I don't know which
one is better. Any comments from others please.

// polytest.cpp
#include <iostream.h>

class	Point {
	public:
		Point( int xx = 0, int yy = 0 ) : x(xx), y(yy) { cout << "Point()" << endl; }
		Point( const Point& p ) : x(p.x), y(p.y) { cout << "Point(XX&)" << endl; }
		virtual ~Point() { cout << "~Point()" << endl; }
		virtual void Show() { cout << "Point.Show()" << endl; }
		virtual void Hide() { cout << "Point.Hide()" << endl; }
		void MoveTo( int xx, int yy ) {
			Hide();         // the correct Hide() method will be established at runtime
			x = xx; y = yy; // this is the same for every class
			Show();         // the correct Show() method will be established at runtime
		}
	protected:
		int x;
		int y;
};
class Circle : public Point {
	public:
		Circle( int xx = 0, int yy = 0, int rr = 0 ) : Point(xx, yy), r(rr) { cout << "Circle()" << endl; }
		Circle( const Circle& c ) : Point(c.x, c.y), r(c.r) { cout << "Circle(XX&)" << endl; }
		virtual ~Circle() { cout << "~Circle()" << endl; }
		virtual void Show() { cout << "Circle.Show()" << endl; }
		virtual void Hide() { cout << "Circle.Hide()" << endl; }
	protected:
		int r;  // radius
};
class Rectangle : public Point {
	public:
		Rectangle( int xx = 0, int yy = 0, int ww = 0, int hh = 0 ) : Point(xx, yy), w(ww), h(hh) { cout << "Rectangle()" << endl; }
		Rectangle( const Rectangle& r ) : Point(r.x, r.y), w(r.w), h(r.h) { cout << "Rectangle(XX&)" << endl; }
		virtual ~Rectangle() { cout << "~Rectangle()" << endl; }
		virtual void Show() { cout << "Rectangle.Show()" << endl; }
		virtual void Hide() { cout << "Rectangle.Hide()" << endl; }
	protected:
		int w;  // width
		int h;  // height
};
void showfunction( Point * p ) {
	p->Show();
}
int main() {
	Point * X;
	X = new Point(10, 10);
	X->Show();
	X->MoveTo(20, 20);
	X->Hide();
	delete X;
	X = new Circle(10, 10, 100);
	X->Show();
	X->MoveTo(20, 20);
	X->Hide();
	delete X;
	X = new Rectangle(10, 10, 100, 200);
	X->Show();
	X->MoveTo(20, 20);
	X->Hide();
	delete X;

// !!!  N E W !!!
	cout << endl << "New:" << endl;

	showfunction( &Rectangle(10,10,100,200) );
	cout << "This seems to work." << endl;

	X = new Rectangle(10, 10, 100, 200);
	showfunction(X);
	delete X;
	cout << "This also seems to work." << endl;

	return 0;
}
// eof

Guido.
--------------------------------------------------------------------
- Guido Schmidt                                                    -
- schmidt@rz.uni-passau.de                                         -
- Universitaet Passau, Germany                                     -
--------------------------------------------------------------------