Re: lug-bg: c/c++ differences
- Subject: Re: lug-bg: c/c++ differences
- From: megaface@email.domain.hidden (Nikolay Mitev)
- Date: Fri, 4 Jul 2003 17:43:26 +0200 (MEST)
Hi,
<em class="quotelev1">> i wse pak reshenie imalo za koeto ne se bqh setil
<em class="quotelev1">> extern "C"
<em class="quotelev1">> samo deto trqbwa da poexperimentiram s towa
<em class="quotelev1">> zashtoto ne sym siguren kakwo shte se poluchi
<em class="quotelev1">> s polimorfizma :)
<em class="quotelev1">> 10x za otgowora wse pak :)
<em class="quotelev1">>
kakuv polimorphism te goni be 4ovek? Nqma takuv film tuka. Ima6 ptr kum
funkciq. Nqma6 obekt, nqma6 ni6to. Kakuv polymorphism bez obekt? Eti te
elegantno re6enie na problema ti s polymorphism.
main program:
obtest.h
======
class Test {
public:
virtual void do_something () const {
std::cout << "Shit, no polymorphism :-(" << std::endl;
}
};
======
main.cpp
======
#include <iostream>
#include <dlfcn.h>
#include "obtest.h"
int main () {
void *d = dlopen ("libtest.so", RTLD_NOW);
if (d == 0) {
std::cout << "Can't find library" << std::endl;
}
Test* (*object_factory) () = (Test* (*) ()) dlsym (d, "object_factory");
Test* t;
if (object_factory != 0) {
t = object_factory ();
} else {
std::cout << "Can't find function" << std::endl;
exit (1);
}
t->do_something ();
}
======
plugin:
test.cpp
======
#include <iostream>
#include "obtest.h"
class Test1 : public Test {
public:
virtual void do_something () const {
std::cout << "Wow, polymorphism" << std::endl;
}
};
tatic Test1 test;
extern "C" {
Test* object_factory () {
return &test;
}
}
======
test.cpp kompilira6 s g++ -shared -olibtest.so test.cpp
drugoto g++ -otest main.cpp i runva6.
Kratko obqsnenie.:
Izpolzva6 plugin-a kato faktory za obekti. Func-a ti vru6ta ptr KUM OBEKT.
Prez nego kato vikne6 funkciq ima6 ve4e polymorphism v deijstvie.
N.B. Naro4no test v plugina e static, za6toto ne e hubavo da se zadelq pamet
v dll i da se osvobozhdava v main, naprimer, za6toto mozhe dll-a i app-a da
izpolzvat razli4ni heap-ove i da stane boza. Zatova ako zadelq6 v factory-to
dinami4no, napravi i o6te edna func destroy (void *), naprimer, v plugin-a, s
koqto da osvobozhdava6 zadelenata pamet pak v konteksta na plugin-a.
E, dano sum pomognal.
cheers,
face
<p><p><p>
--
+++ GMX - Mail, Messaging & more http://www.gmx.net +++
Jetzt ein- oder umsteigen und USB-Speicheruhr als Prämie sichern!
============================================================================
A mail-list of Linux Users Group - Bulgaria (bulgarian linuxers).
http://www.linux-bulgaria.org - Hosted by Internet Group Ltd. - Stara Zagora
To unsubscribe: http://www.linux-bulgaria.org/public/mail_list.html
============================================================================
|