dev.gamez.lv Forum Index dev.gamez.lv
Latvian Game Developers Community
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups 

Kā uzkodēt smuku interfeisa klasi iekš C++?

 
dev.gamez.lv Forum Index -> Programmēšana
View previous topic :: View next topic  
Author Message
Lamerz
Lameris
Lameris


Joined: 09 Sep 2005
Posts: 99
Location: Rīga

PostPosted: Thu Oct 04, 2007 2:48 pm    Post subject: Kā uzkodēt smuku interfeisa klasi iekš C++?

Labdien!
Gribās uztaisīt tādu kā smuku interfeisiņu un iedabūt iekš DLL. Respektīvi, lai klientam ir pieejams tikai Interface.h hederis. Biju pārliecināts, ka zemāk redzamais kods varētu darboties, bet linkeris tā nedomā - saka, ka GetInterface ir 'unresolved external symbol'.

Var maz tā izdarīt kā man gribās?

Code:
/* Interface.h */
class __declspec(dllexport) Interface
{
public:
   static Interface * GetInterface();
   virtual ~Interface() {}
   virtual void DoSomething1() = 0;
};

Code:
/* Implementation.h */

#include "Interface.h"

class Implementation : public Interface
{
public:
   Implementation();
   virtual ~Implementation();
   static Interface * GetInterface();
   virtual void DoSomething1();
};


Viss linkojamais ir pielinkots. Man domāt, ka kods nav riktīgs...


Last edited by Lamerz on Thu Oct 04, 2007 3:45 pm; edited 1 time in total
Back to top
View user's profile
snake5
Indago dalībnieks
Indago dalībnieks


Joined: 27 Jun 2007
Posts: 2590

PostPosted: Thu Oct 04, 2007 3:39 pm    Post subject:

paņem .lib failu no dll'a un iekopē compilētāja library'u mapē
un pielinko exefailam arī to!
_________________
"There are two choices here: "looks good" and "realism"." -- Paul Nettle
Back to top
View user's profile Visit poster's website
bubu
Indago Uzvarētājs
Indago Uzvarētājs


Joined: 23 Mar 2004
Posts: 3223
Location: Riga

PostPosted: Thu Oct 04, 2007 3:48 pm    Post subject:

Tev nav definēta kompilētajā dll failā funkcija Interface::GetInterface(). Tā kā tā nav virtuāla, tad tā nevar tikt overridota apakšklasēs.
Back to top
View user's profile Send e-mail
Lamerz
Lameris
Lameris


Joined: 09 Sep 2005
Posts: 99
Location: Rīga

PostPosted: Thu Oct 04, 2007 4:03 pm    Post subject:

Diemžēl kompilators žēlojas, ka "'virtual' cannot be used with 'static'". Doma bija tāda, lai klients var lietot klasi tikai caur interfeisu, kur GetInterface izveido klases, kas implementē interfeisu, instanci un atgriež rādītāju uz interfeisu. Tipa:
Code:
interfaceToMyClass = Interface::GetInterface();
interfaceToMyClass->DoSomething1();
...vai es esmu galīgi sasapņojies?
Back to top
View user's profile
bubu
Indago Uzvarētājs
Indago Uzvarētājs


Joined: 23 Mar 2004
Posts: 3223
Location: Riga

PostPosted: Thu Oct 04, 2007 4:06 pm    Post subject:

Jā, esi galīgi sasapņojies :)
Nevarēs tā.
static pie metodes definē globālu funkciju, kas nepieder nevienai klases instance (tai nav nekāda sakara ar apakšklašu objektiem). Turpretī virtual attiecās uz klases instancēm (objektiem), tas norāda, ka metode var tik overraidota apakšklasēs.
Back to top
View user's profile Send e-mail
Lamerz
Lameris
Lameris


Joined: 09 Sep 2005
Posts: 99
Location: Rīga

PostPosted: Thu Oct 04, 2007 4:21 pm    Post subject:

Skaidrs. Paldies!
Varbūt rakstīt kādu atsevišķu, globālu funkciju, kas instancētu objektu un klientam iedotu interfeisu vai nolikvidēt to interfeisu uz velna paraušanu?...
Back to top
View user's profile
bubu
Indago Uzvarētājs
Indago Uzvarētājs


Joined: 23 Mar 2004
Posts: 3223
Location: Riga

PostPosted: Thu Oct 04, 2007 4:30 pm    Post subject:

Ja tev dll failī ir viena māster klase, kura jāinstancē vienmēr (nu tipa spēles engine), tad var darīt līdzīgi kā Irrlicht dara - exportē tikai vienu funkciju no dlļa - Device* createDevice(...); Un savukārt Device klasei ir virtuālas metodes, kuras izveido visus pārējās vajadzīgās klases. Pasties Irrlichta sourci.

Vēl cits variants (pretīgāks) - taisi enumu ar visu vajadzīgo klašu ID, un tad tajā vienā statiskajā GetInterface(InterfaceID id) metodē taisi kaudzi ar if'iem/switchu: if (id==KlaseA) { return new KlaseA(); } else if (id==KlaseB) { return new KlaseB(); }
Šo otro variantu var nedaudz kautkā mēģināt automatizēt ar std::map un funkciju pointeriem, bet nu ļoti skaisti tas neizskatīsies.
Back to top
View user's profile Send e-mail
Display posts from previous:   
dev.gamez.lv Forum Index -> Programmēšana All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group