C++ me g++
Tassos Bassoukos
abas at aix.meng.auth.gr
Fri May 4 11:49:01 EEST 2001
On Thu, 3 May 2001, Konstantinos Pistiolis wrote:
> Edw yparxei ena kolpo. Mporeis na kaleseis mia
> synarthsh klassews XWRIS NA EXEIS KANENA ANTIKEIMENO,
> arkei h synarthsh ths klassews na einai 'static'
> an eixes dhladh
> class AClass{
> static int myFunc();
> };
> tote OLA OSA H8ELES NA KANEIS APO TIN ARXH GINONTAI
> (xwris dhladh .*). To mega problima einai omws oti oi
> static synarthseis den mporoyn na 'doyn' mh static
> melh ths klassews, opote h kaneis olh thn klash static
> h ftiaxneis mia asxeth synarthsh gia th signal() poy
> 8a kalei th mh statikh myFunc() panw sto monadiko
> antikeimeno soy (proteino).
> Mporeis epishs na baleis ton 'this' pointer ws statikh
> metablhth sthn klassh soy, kai h statikh myFunc() 8a
> kalei ta alla melh ws thisPrt->...
(Prospa8w entonotata na mhn ma8w tipota pou na exei sxesh me to bdelygma
pou legetai C++, opote mallon o kwdikas den 8a douleyei. Thn idea omws
elpizw na thn katalabete.)
Na proteinw mia pio kompsh lysh ?
Kane thn klash Singleton, me enan private constructor kai me mia
public static getInstance() methodo, h opoia eite 8a dhmiourgei to
monadiko instance, eite 8a to epistrefei. O signal handler sou 8a einai
static (opws swsta anafer8hke), kai h idia h me8odos den 8a kanei tipote
allo apo to na briskei to Singleton kai na kalei thn me8odo tou pou 8a
kanei thn pragmatikh douleia. Des kalytera ton kwdika, to deixnei
kalytera... :-)
class AClass {
public static AClass getInstance();
static void signal_handler();
void myFunc();
}
static AClass AClass::getInstance() {
static AClass singleton_instance=NULL;
if(singleton_instance==NULL){
singleton_instance=new AClass;
}
return singleton_instance;
}
static void AClass::signal_handler() {
AClass::getInstance()->myFunc();
}
void AClass::myFunc() {
....
}
HTH, HAND,
Tassos
--
Bassoukos Tassos +30 31 996011 / +30 93 7109954 IT Generalist
When you design user interfaces, it's a good idea to keep two principles in mind:
1.Users don't have the manual, and if they did, they wouldn't read it.
2.In fact, users can't read anything, and if they could, they wouldn't want to.
More information about the Linux-greek-users
mailing list