Raised This Month: $ Target: $400
 0% 

Creating instantiable interface classes?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Chrisber
AlliedModders Donor
Join Date: Jul 2007
Location: localhost
Old 04-22-2010 , 14:06   Creating instantiable interface classes?
Reply With Quote #1

Hey.
On this tutorial I've learned how to create interfaces which can't be instantiated: http://www.codeguru.com/cpp/cpp/cpp_...icle.php/c9989
That's nice for example managing classes, e.g. a PlayerManager. But what's about instantiable interface classes? Is that possible in anyway? For example to create an instantiable class called ConVar (this is only a pseudo name ).

Thanks in advance!
Chrisber is offline
BAILOPAN
Join Date: Jan 2004
Old 04-23-2010 , 00:04   Re: Creating instantiable interface classes?
Reply With Quote #2

Gross.

Just code in a way that makes sense, don't worry about inheritance hierarchies. If you want to instantiate it, don't include pure virtual methods. Don't bother with "interfaces" or virtual unless you need to expose an API (or inherit implementation, if you really need it).

I guess what I'm saying is that it's really easy to overdo inheritance (i.e. Valve's CBaseEntity rats' nest), be careful.
__________________
egg

Last edited by BAILOPAN; 04-23-2010 at 00:07.
BAILOPAN is offline
API
Veteran Member
Join Date: May 2006
Old 04-25-2010 , 21:16   Re: Creating instantiable interface classes?
Reply With Quote #3

That tutorial just seems like a few macros. Also, what do you mean by instantiable? I want to help, I just don't understand.
__________________
API is offline
Send a message via AIM to API
Chrisber
AlliedModders Donor
Join Date: Jul 2007
Location: localhost
Old 04-26-2010 , 09:26   Re: Creating instantiable interface classes?
Reply With Quote #4

Mhh.
I try to explain again (sry, my englisch is may a bit of lack because I'm german).

You know there are interface classes which are only instantiated by the engine, for example IVEngineServer. Every plugin can call functions from it without having the sourceode of it, only the headerfiles are available.

Then, there are classes like bf_write which can't be instantiated by the plugin, but from the engine through a wrapper function (IVEngineServer::UserMessageBegin which returns a valid pointer to a bf_write instance). Also, the plugin author only has the header files available, not the source.

Then - this is what I want to do now - there should be a class which the plugin can instantiate by itself, without a wrapper function. The problem is currently, that this is only possible when the plugin has the sourcecode available. But that should'nt be, only header files should be published.
An example for this is a string class. It would get on everyones nervs if you alway have to type "pBlah->CreateString("blah")" which returns a valid pointer to a string instance.

Do you understand now?

Thanks!
Chrisber is offline
API
Veteran Member
Join Date: May 2006
Old 04-27-2010 , 22:40   Re: Creating instantiable interface classes?
Reply With Quote #5

Ah, I understand what you are saying now. Let me explain how interfaces working in C++ first. The header file usually looks something like this:
Code:
class ISomeFace
{
public:
  virtual ~ISomeFace();
  virtual void SomeMethod() = 0;
};
The above class is an abstract class. Inside the game plugin itself, there would be a class like this:
Code:
class cSomeFace : public ISomeFace
{
public:
  cSomeFace() { /* code */ }
  virtual ~cSomeFace() { /* code */ }
  virtual void SomeMethod() { /* code goes here */ }
};
Now, if you tried to create an instance of ISomeFace, you would get an error:
Code:
ISomeFace *pFace = new ISomeFace();
1) There isn't a constructor for the class. This is because it is abstract.
2) Abstract classes can not be initialized. They are simply a virtual table.
3) Inside your server plugin, you should define a function like this:
Code:
ISomeFace *CreateAFace()
{
    cSomeFace *pFace = new cSomeFace(); // This is valid!
    return (ISomeFace*) pFace;
}
Now, since the pointer returned is actually an object that over-rides the methods contained in ISomeFace, the virtual table will be able to determine the correct call of functions. For example, if another C++ program wanted to use your interface:
Code:
ISomeFace *pFace = InterfaceFactory->CreateAFace();
You might be asking, how do we get to this point without being able to initialize an interface? Well, if you have ever used a DLL export, then the process isn't too difficult to understand. Programs are able to export functions, I believe in SourceMod there is actually a macro in the sdk include file that helps you avoid this practice. GetSMAPI or something like that. Well, what I like doing is handing off a single instance of what I like to call an interface factory. The interface factory is the only thing that has to be exported/imported.

In sumation, interfaces are not initialized by the third party for a reason. They are only an abstract representation (AKA a virtual table)

Please let me know if this is hard to understand, I know I am not the best teacher.
__________________
API is offline
Send a message via AIM to API
Chrisber
AlliedModders Donor
Join Date: Jul 2007
Location: localhost
Old 04-28-2010 , 04:15   Re: Creating instantiable interface classes?
Reply With Quote #6

Hey.
Thanks!
I understand that, and if I'm right, my plan isn't to realize without
a) creating a wrapper which returns an instance
or
b) publishing the sorucecode of classes which the plugin can instantiate itself.

Right?

Chris
Chrisber is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:26.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode