Having trouble with objects.

Steven Schveighoffer schveiguy at yahoo.com
Tue Mar 18 09:00:29 PDT 2008

"Jarrod" wrote
> Hey all.
> As I might have mentioned a while ago, I have a config library that I
> wrapped in D classes a while ago.
> While it works perfectly fine, it's currently running somewhat
> inefficiently and I wish to fix this.
> The problem: The library currently contains two classes. A root class and
> a node class. To load/create a config, one would create a root class and
> tie it to a file. Each key=value pair in the config can be accessed by
> passing the value's "path" to the root object, which will return a node.
> Herein lies the issue.
> Nodes are very short lived objects, and are often created and discarded
> very quickly. It would make far more sense if a node was a struct, as
> classes being uses like this tend to needlessly waste memory. The problem
> is I need to create the node in the root object (or in a parent node) and
> pass it back to the caller which would be more efficient to do with
> classes. In C++, what I would do is pass the back argument to the
> struct's constructor, which would create the struct and pass it back
> rather efficiently, and the node would be destroyed when scope is left. I
> doubt this is possible in D since structs don't have constructors.
> So what should I do?

Have you tried static opCall?  It's sort of like a struct constructor:

struct S
  int val;
  static S opCall(int arg)
      S retval;
      retval.val = arg;
      return retval;

S func()
    return S(1);


More information about the Digitalmars-d-learn mailing list