www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Interfaces and member variables

reply Charlie <Charlie_member pathlink.com> writes:
Example

interface ISubmitter 
{
// Socket socket; 
// not allowed

bit Login();
bit Submit();
}

class Submitter
{
Socket socket;
abstract bit Login();
abstract bit Submit();

}

This isnt allowed ( Submitter doesn't implement function Login ... ) , how can i
accomplish the same thing ?  ( all classes that derive from submitter have a
Socket member )  Other than making them empty functions or { assert(0); } ?

Thanks,
Charlie
Jul 29 2004
next sibling parent Derek Parnell <derek psych.ward> writes:
On Thu, 29 Jul 2004 21:36:44 +0000 (UTC), Charlie wrote:

I'm a bit slow, ok. I don't actually understand much of what you are
writing about here. 

 Example
 
 interface ISubmitter 
 {
 // Socket socket; 
 // not allowed
The above is not allowed because interfaces can only include unadorned member functions.
 bit Login();
 bit Submit();
 }
 
 class Submitter
 {
 Socket socket;
 abstract bit Login();
 abstract bit Submit();
 
 }
 
 This isnt allowed ( Submitter doesn't implement function Login ... )
What is the 'it' that isn't allowed? Do you mean that the compiler is not allowing it or that you do not want it to be allowed?
 , how can i
 accomplish the same thing ?  ( all classes that derive from submitter have a
 Socket member )  Other than making them empty functions or { assert(0); } ?
'Socket' is not a function. So how would "making them empty functions or { assert(0); }" help? Are you wanting Submitter to implement the interface "ISubmitter"? Anyhow, here is some code that might help... <code> class Socket { int x; } interface ISubmitter { bit Login(); bit Submit(); } class Submitter : ISubmitter { Socket socket; this() { socket = new Socket; } abstract bit Login(); abstract bit Submit(); } class mySubmitter: Submitter { bit Login() { return 1;} bit Submit() { return 0; } } void main() { mySubmitter a = new mySubmitter; a.socket.x = 1; } </code> This compiles and the derived class has a Socket member. -- Derek Melbourne, Australia 30/Jul/04 10:41:11 AM
Jul 29 2004
prev sibling parent parabolis <parabolis softhome.net> writes:
Charlie wrote:
 Example
 
 interface ISubmitter 
 {
 // Socket socket; 
 // not allowed
 
 bit Login();
 bit Submit();
 }
 
 class Submitter
 {
 Socket socket;
 abstract bit Login();
 abstract bit Submit();
 
 }
 
 This isnt allowed ( Submitter doesn't implement function Login ... ) , how can
i
 accomplish the same thing ?  ( all classes that derive from submitter have a
 Socket member )  Other than making them empty functions or { assert(0); } ?
 
 Thanks,
 Charlie
 
It sounds like you want an abstract class instead of an Interface for ISubmitter? abstract class ACSubmitter { Socket socket; // allowed! abstract bit Login(); abstract bit Submit(); } abstract class Submitter : Submitter { // Socket socket; <- already have this abstract bit Login(); abstract bit Submit(); }
Jul 29 2004