How do i depend on struct composition

JS js.mdnq at gmail.com
Thu Aug 22 15:32:48 PDT 2013


On Thursday, 22 August 2013 at 21:07:27 UTC, Mariusz `shd` 
Gliwiński wrote:
> Let me try to illustrate my question by following pseudo-code:
>
> struct InnerA
> {
>   /* .. */
>   void* ptr;
> }
> struct InnerB
> {
>   /* .. */
>   void* ptr;
> }
> struct InnerC
> {
>   void operate()
>   {
>     auto ptr = this-(void*).sizeof;
>     /* do something with pointer */
>   }
> }
>
> struct Compound(Inner)
> {
>   Inner  a_or_b;
>   InnerC c;
> }
>
> I need to pass "c" to some subsystem which shouldn't be aware 
> of "a_or_b"
>
> However, subsystem should be able to call function 
> InnerB.operate() which needs access to "a_or_b".
>
> Now my questions:
> * Is there any way to tell subsystem, that he can't instantiate 
> nor move InnerC
> * Is there any better,platform-independent way of accessing 
> pointer from struct a_or_b ?
>
> Thanks in advance,
> Mariusz

If you actually nest InnerC inside Compound then it will contain 
a pointer(using this) to the parent. You can then use `parent` 
inside `operator` to access the parent which can access a_or_b.

if you need to return a member from a_or_b without going through 
a_or_b then just wrap it.

I'm not sure exactly what you are trying to do but I think it can 
easily be done with standard techniques.


More information about the Digitalmars-d-learn mailing list