June 24, 2004
Norbert Nemec wrote:
> Jan-Eric Duden wrote:
> 
>>Are there better solutions for doing this kind of stuff in D?
> 
> 
> Use classes!
classes are on the heap, structs can be on the stack--this is important--esp if you're doing something like a number sort of class or a vector
> 
> The only reason to use struct instead of class is, if you want
> value-semantics (as opposed to reference semantics which are the only
> available behaviour for classes in D)
> 
> Polymorphism is generally only possible when handling references. (Since the
> size of an inheriting class is not known, it cannot be passed by value)
> 
> Your C++ example uses a pointer to a struct. In D, you should simply use a
> class for that.
> 
June 24, 2004
"Norbert Nemec" <Norbert.Nemec@gmx.de> wrote in message news:cbdq61$2u6v$1@digitaldaemon.com...
> Regan Heath wrote:
>
> > This:
> >
> > struct a {
> > }
> >
> > struct b : a {
> > }
> >
> > is not valid, the spec does not seem to allow it, why not?
>
> My guess:
>
> Structs cannot have virtual functions (adding that feature would mean overhead which should be avoided for structs)
>
> Without virtual functions, the behavior of struct-inheritance would be strangely different from the behavior of class-inheritance, where every public function is implicitly virtual.
>
> Furthermore: without polymorphism (which would require virtual functions) inheritance is just a matter of saving some typing. And for that, you can also use mixins.

Very nice point! I knew there was a point to trawling the posts tonight.

Thanks for that. :)


July 01, 2004
You can do the following:

struct A { ... }

struct B
{
    A a;
    ... more members ...
}


July 01, 2004
"Walter" <newshound@digitalmars.com> wrote in message news:cc0hlu$9gv$2@digitaldaemon.com...
>
> You can do the following:
>
> struct A { ... }
>
> struct B
> {
>     A a;
>     ... more members ...
> }
>

Or even (maybe) better:

struct A() { }

struct B
{
    mixin A!();
 //   ... more members ...
}




July 01, 2004
In article <opr92xf8ba5a2sq9@digitalmars.com>, Regan Heath says...
>
>This:
>
>struct a {
>}
>
>struct b : a {
>}
>
>is not valid, the spec does not seem to allow it, why not?
>
>Regan
>
>-- 
>Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

It isn't allowed because structs in d are c style structs not c++ style stucts. In C the struct makes a data aggregate. For C++ this is not the case. The struct key word is equivalent to class foo{ public:}  by that I mean it creates a class which defaults to public instead of private.  So it has all of that over head you wish to avoid.


July 01, 2004
On Thu, 1 Jul 2004 14:12:37 +0200, Ivan Senji <ivan.senji@public.srce.hr> wrote:

> "Walter" <newshound@digitalmars.com> wrote in message
> news:cc0hlu$9gv$2@digitaldaemon.com...
>>
>> You can do the following:
>>
>> struct A { ... }
>>
>> struct B
>> {
>>     A a;
>>     ... more members ...
>> }
>>
>
> Or even (maybe) better:
>
> struct A() { }
>
> struct B
> {
>     mixin A!();
>  //   ... more members ...
> }
>

Nope: mixin A!() A is not a template

Besides which you cannot use a mixin to add private members.

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
July 01, 2004
On Thu, 1 Jul 2004 01:11:15 -0700, Walter <newshound@digitalmars.com> wrote:

>
> You can do the following:
>
> struct A { ... }
>
> struct B
> {
>     A a;
>     ... more members ...
> }

That's not the same.

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
July 01, 2004
On Thu, 1 Jul 2004 15:15:36 +0000 (UTC), Brian <Brian_member@pathlink.com> wrote:

> In article <opr92xf8ba5a2sq9@digitalmars.com>, Regan Heath says...
>>
>> This:
>>
>> struct a {
>> }
>>
>> struct b : a {
>> }
>>
>> is not valid, the spec does not seem to allow it, why not?
>>
>> Regan
>>
>> --
>> Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
>
> It isn't allowed because structs in d are c style structs not c++ style stucts.

I know. Currently, they are...

> In C the struct makes a data aggregate. For C++ this is not the case. The struct
> key word is equivalent to class foo{ public:}  by that I mean it creates a class
> which defaults to public instead of private.  So it has all of that over head
> you wish to avoid.

The overhead comes when it's implemented as you suggest.

You can implement structs with inheritance and without virtual functions to remove all that overhead.

similar to:

struct A {
}
struct B {
  mixin A;
}

however there are 2 problems with this soln.

1. it does not work, you cannot mixin a struct.
2. you cannot use a mixin to add 'private' members.


What I want is to be able to say:

struct A {
  virtual void bar();
private:
  void foo();
}

struct B : A {
  void bar() {
    foo();
  }
}

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
July 01, 2004
On Fri, 02 Jul 2004 10:07:51 +1200, Regan Heath <regan@netwin.co.nz> wrote:

> On Thu, 1 Jul 2004 15:15:36 +0000 (UTC), Brian <Brian_member@pathlink.com> wrote:
>
>> In article <opr92xf8ba5a2sq9@digitalmars.com>, Regan Heath says...
>>>
>>> This:
>>>
>>> struct a {
>>> }
>>>
>>> struct b : a {
>>> }
>>>
>>> is not valid, the spec does not seem to allow it, why not?
>>>
>>> Regan
>>>
>>> --
>>> Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
>>
>> It isn't allowed because structs in d are c style structs not c++ style stucts.
>
> I know. Currently, they are...
>
>> In C the struct makes a data aggregate. For C++ this is not the case. The struct
>> key word is equivalent to class foo{ public:}  by that I mean it creates a class
>> which defaults to public instead of private.  So it has all of that over head
>> you wish to avoid.
>
> The overhead comes when it's implemented as you suggest.
>
> You can implement structs with inheritance and without virtual functions to remove all that overhead.
>
> similar to:
>
> struct A {
> }
> struct B {
>    mixin A;
> }
>
> however there are 2 problems with this soln.
>
> 1. it does not work, you cannot mixin a struct.
> 2. you cannot use a mixin to add 'private' members.
>
>
> What I want is to be able to say:
>
> struct A {
>    virtual void bar();
> private:
>    void foo();
> }
>
> struct B : A {
>    void bar() {
>      foo();
>    }
> }

Small correction, there should be a body in the void foo() function in struct A.

My current workaround is to make A a template, and to make foo protected rather than private.

Regards,
Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
July 02, 2004
On Thu, 1 Jul 2004 01:11:15 -0700, Walter <newshound@digitalmars.com> wrote:

>
> You can do the following:
>
> struct A { ... }
>
> struct B
> {
>     A a;
>     ... more members ...
> }

Should this work?

--[test.d]--
struct A {
	int aaa;
	int bbb;
}
struct B {
	A a_instance;
	alias a_instance.aaa aaa;
	alias a_instance.bbb bbb;	
}

void main() {
	B b;
	
	b.aaa = 5;
	b.bbb = 6;
}

Cos it gives:

test.d(7): identifier 'aaa' of 'a_instance.aaa' is not defined

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/