Thread overview | |||||
---|---|---|---|---|---|
|
July 29, 2004 Interfaces and member variables | ||||
---|---|---|---|---|
| ||||
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 |
July 30, 2004 Re: Interfaces and member variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charlie | 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 |
July 30, 2004 Re: Interfaces and member variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charlie | 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();
}
|
Copyright © 1999-2021 by the D Language Foundation