www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - auto members of classes (bug + suggestion)

reply Nick <Nick_member pathlink.com> writes:
Take a peek at the following code:

class test {}

class test2
{
auto test T;
}

void main() {}

The 'auto' in this example is not supposed to be "legal", since T is a class
member. Yet the compiler doesn't seem to complain about it and the above
compiles fine.(However, if we make the above class 'test' an auto class, the
compiler suddenly starts complaining...)

Ok, so that was the bug, now for the suggestion:

In C++ you can declare class members in the following way:
class test2
{
test T;
..
};

When an object of class test2 goes out of scope, T is automatically destructed.
You can't really do this in D, without adding delete statements to the
destructor. So how about having auto members automatically destruct when the
owner destructs, i.e. making "auto test T" in the above example equivalent with
adding a "delete T" in the destructor?

Nick
Jul 22 2004
next sibling parent Sean Kelly <sean f4.ca> writes:
In article <cdp9m0$13lm$1 digitaldaemon.com>, Nick says...
In C++ you can declare class members in the following way:
class test2
{
test T;
..
};

When an object of class test2 goes out of scope, T is automatically destructed.
You can't really do this in D, without adding delete statements to the
destructor. So how about having auto members automatically destruct when the
owner destructs, i.e. making "auto test T" in the above example equivalent with
adding a "delete T" in the destructor?
The idea of partially destructed classes lingering kind of scares me. It means that just passing a class out of a scope will have side effects. And side effects are rarely a good thing :) I think it could be useful if used carefully, but I'm not convinced that this outweighs its potential misuse. Sean
Jul 22 2004
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Nick wrote:
<snip>
 When an object of class test2 goes out of scope, T is automatically destructed.
 You can't really do this in D, without adding delete statements to the
 destructor. So how about having auto members automatically destruct when the
 owner destructs, i.e. making "auto test T" in the above example equivalent with
 adding a "delete T" in the destructor?
Without guaranteed destruction of objects in general, this would break the guaranteed destruction of auto objects. Maybe we could allow auto members to be declared in an auto class. But is there any way we could stop circular data structures being created in this department and hence screwing things up? Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
Jul 23 2004