问一个C++编译出现的问题...简单,拿分请进...
Viali 2005-12-12 07:45:17 //不明白为何会出错..关键是Factory里头的static函数.
#include <iostream>
#include <Wtypes.h>
#include <wincon.h>
using namespace std;
HANDLE console_ = GetStdHandle(STD_OUTPUT_HANDLE);
void gotoxy( int x, int y )
{
COORD coord;
coord.X = x-1;
coord.Y = y-1;
SetConsoleCursorPosition( console_, coord );
}
class Gadget
{
public:
Gadget( char ch ){
ch_ = ch;
}
virtual void draw(long x_, long y_, long w_)
{
int i;
gotoxy( x_, y_ );
for (i=0; i < w_; i++) cout << ch_;
cout << endl;
gotoxy( x_, y_+1 );
cout << ch_;
for (i=0; i < w_-2; i++) cout << ' ';
cout << ch_ << endl;
gotoxy( x_, y_+2 );
for (i=0; i < w_; i++) cout << ch_;
cout << endl;
}
private:
char ch_;
};
class Label : public Gadget
{
public:
Label() : Gadget('@') { }
};
class Edit : public Gadget
{
public:
Edit() : Gadget('#') { }
};
class Factory
{
private:
static Gadget* array[2];
public:
static Gadget* getFlyweight(char* str)
{
if(strcmp(str,"label")==0)
{
if(array[0]==NULL) array[0]=new Label();
return array[0];
}
else if(strcmp(str,"edit")==0)
{
if(array[1]==NULL) array[1]=new Edit();
return array[1];
}
else return NULL;
}
};
int main( void ) {
Gadget* list[6];
list[0] = Factory::getFlyweight( "label" );
system("PAUSE");
}