Thread overview
reference class parent's function
Apr 30, 2007
okibi
Apr 30, 2007
Frits van Bommel
Apr 30, 2007
okibi
Apr 30, 2007
Mike Parker
Apr 30, 2007
okibi
May 01, 2007
okibi
May 01, 2007
okibi
May 03, 2007
Stewart Gordon
April 30, 2007
Hello!

I was wondering if you could tell me how to reference a class's function from another class after the first class has created an instance of the second class. Take a look at this:

class1 {
  class2 c2 = new class2();
  public void writeFunc(char[] myStr) {
    writef("%s", myStr);
  }
}

class2 {
 ???.writeFunc("hello world");
}

Any ideas?

Thanks!
April 30, 2007
okibi wrote:
> Hello!
> 
> I was wondering if you could tell me how to reference a class's function from another class after the first class has created an instance of the second class. Take a look at this:
> 
> class1 {
>   class2 c2 = new class2();
>   public void writeFunc(char[] myStr) {
>     writef("%s", myStr);
>   }
> }
> 
> class2 {
>  ???.writeFunc("hello world"); }
> 
> Any ideas?

Something like the following?
---
import std.stdio;

class class1 {
  class2 c2;
  // init needs to be in constructor since it allocates at run-time
  this() { c2 = new class2(this); }
  public void writeFunc(char[] myStr) {
    writefln("%s", myStr);
  }
}

class class2 {
    class1 c1;

    this(class1 c) { c1 = c; }

    void foo() {
        c1.writeFunc("hello world");
    }
}

void main() {
    auto c = new class1;
    c.c2.foo();
}
---
April 30, 2007
Frits van Bommel Wrote:

> okibi wrote:
> > Hello!
> > 
> > I was wondering if you could tell me how to reference a class's function from another class after the first class has created an instance of the second class. Take a look at this:
> > 
> > class1 {
> >   class2 c2 = new class2();
> >   public void writeFunc(char[] myStr) {
> >     writef("%s", myStr);
> >   }
> > }
> > 
> > class2 {
> >  ???.writeFunc("hello world");
> > }
> > 
> > Any ideas?
> 
> Something like the following?
> ---
> import std.stdio;
> 
> class class1 {
>    class2 c2;
>    // init needs to be in constructor since it allocates at run-time
>    this() { c2 = new class2(this); }
>    public void writeFunc(char[] myStr) {
>      writefln("%s", myStr);
>    }
> }
> 
> class class2 {
>      class1 c1;
> 
>      this(class1 c) { c1 = c; }
> 
>      void foo() {
>          c1.writeFunc("hello world");
>      }
> }
> 
> void main() {
>      auto c = new class1;
>      c.c2.foo();
> }
> ---

That runs at run-time and is all in one module. I need to have it run afterwards and the classes are in separate modules in the program.
April 30, 2007
okibi wrote:

> 
> That runs at run-time and is all in one module. I need to have it run afterwards and the classes are in separate modules in the program.

You can execute functions at run time or at compile time. What do you mean by 'run afterwards'? As for the module problem, what's wrong with just importing the other module?
April 30, 2007
Mike Parker Wrote:

> okibi wrote:
> 
> > 
> > That runs at run-time and is all in one module. I need to have it run afterwards and the classes are in separate modules in the program.
> 
> You can execute functions at run time or at compile time. What do you mean by 'run afterwards'? As for the module problem, what's wrong with just importing the other module?

This is a gtkD application and by after I mean after everything has drawn and is waiting for you to do something. And I am importing the module but it doesn't recognize the first class.
May 01, 2007
okibi Wrote:

> Mike Parker Wrote:
> 
> > okibi wrote:
> > 
> > > 
> > > That runs at run-time and is all in one module. I need to have it run afterwards and the classes are in separate modules in the program.
> > 
> > You can execute functions at run time or at compile time. What do you mean by 'run afterwards'? As for the module problem, what's wrong with just importing the other module?
> 
> This is a gtkD application and by after I mean after everything has drawn and is waiting for you to do something. And I am importing the module but it doesn't recognize the first class.

Nobody has any ideas?
May 01, 2007
okibi Wrote:

> okibi Wrote:
> 
> > Mike Parker Wrote:
> > 
> > > okibi wrote:
> > > 
> > > > 
> > > > That runs at run-time and is all in one module. I need to have it run afterwards and the classes are in separate modules in the program.
> > > 
> > > You can execute functions at run time or at compile time. What do you mean by 'run afterwards'? As for the module problem, what's wrong with just importing the other module?
> > 
> > This is a gtkD application and by after I mean after everything has drawn and is waiting for you to do something. And I am importing the module but it doesn't recognize the first class.
> 
> Nobody has any ideas?

Is it just because the second class in in a separate module that gets imported? If I put both classes in the same module, it works fine. Seems to me there would be any easy way for the two to talk seeing as there are in the same program.
May 03, 2007
"okibi" <okibi@ratedo.com> wrote in message news:f17r27$28ku$1@digitalmars.com...
<snip>
> Nobody has any ideas?

I have a few that will help you a lot in general:

1. Post the exact code that you're trying.  If it's quite long, trim it to a minimal version that you've tested and found to show the same problem.

2. Post the exact error messages you receive when you try the code you've posted.

As it happens, this works for me:

----- okibi1.d -----
import okibi2;
import std.stdio;

class class1 {
  class2 c2;
  // init needs to be in constructor since it allocates at run-time
  this() { c2 = new class2(this); }
  public void writeFunc(char[] myStr) {
    writefln("%s", myStr);
  }
}


void main() {
    auto c = new class1;
    c.c2.foo();
}
----- okibi2.d -----
import okibi1;

class class2 {
    class1 c1;

    this(class1 c) { c1 = c; }

    void foo() {
        c1.writeFunc("hello world");
    }
}
----------

Stewart.