www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - reference class parent's function

reply okibi <okibi ratedo.com> writes:
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!
Apr 30 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
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(); } ---
Apr 30 2007
parent reply okibi <okibi ratedo.com> writes:
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.
Apr 30 2007
parent reply Mike Parker <aldacron71 yahoo.com> writes:
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?
Apr 30 2007
parent reply okibi <okibi ratedo.com> writes:
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.
Apr 30 2007
parent reply okibi <okibi ratedo.com> writes:
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
next sibling parent okibi <okibi ratedo.com> writes:
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 01 2007
prev sibling parent "Stewart Gordon" <smjg_1998 yahoo.com> writes:
"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.
May 02 2007