Accessing a field of a containing class from within a nested class
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Apr 1 15:03:08 PDT 2015
On 04/01/2015 11:25 AM, Charles Hixson via Digitalmars-d-learn wrote:>
The class Node is contained within the struct BTree.
> The field btFile is contained within the struct BTree.
> The statement is within a function within the Node class.
>
> I've tried many variations, here are a few:
>
> btFile.write(self.nodeId, cast(void*)&(self));
> results in:
> need 'this' for 'btFile' of type 'BlockFile'
>
> this.btFile.write(self.nodeId, cast(void*)&(self));
> results in:
> Error: no property 'btFile' for type 'btplus.BTree.Node'
>
> this.BTree.btFile.write(self.nodeId, cast(void*)&(self));
> results in:
> Error: constructor btplus.BTree.this (string fName) is not callable
> using argument types (Node)
>
> Perhaps BTree needs to be a class? I made it a struct because I want it
> to definitely close properly when it
> goes out of scope.
Can you modify the definition of Node? If so, perhaps it's possible
construct Node objects with a reference to its btFile:
import std.stdio;
class Node
{
int *btFile;
this(int *btFile)
{
this.btFile = btFile;
}
void foo()
{
writeln(*btFile);
}
}
struct BTree
{
int btFile;
Node node;
this(int btFile)
{
this.btFile = btFile;
this.node = new Node(&this.btFile);
}
}
void main()
{
auto bt = BTree(42);
bt.node.foo();
}
Ali
More information about the Digitalmars-d-learn
mailing list