digitalmars.D.learn - reference class parent's function
- okibi (13/13) Apr 30 2007 Hello!
- Frits van Bommel (24/40) Apr 30 2007 Something like the following?
- okibi (2/48) Apr 30 2007 That runs at run-time and is all in one module. I need to have it run af...
- Mike Parker (4/6) Apr 30 2007 You can execute functions at run time or at compile time. What do you
- okibi (2/10) Apr 30 2007 This is a gtkD application and by after I mean after everything has draw...
- okibi (2/14) May 01 2007 Nobody has any ideas?
- okibi (2/18) May 01 2007 Is it just because the second class in in a separate module that gets im...
-
Stewart Gordon
(35/36)
May 02 2007
"okibi"
wrote in message
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
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
Frits van Bommel 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.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
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
Mike Parker Wrote:okibi wrote: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.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
okibi Wrote:Mike Parker Wrote:Nobody has any ideas?okibi wrote: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.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?
May 01 2007
okibi Wrote:okibi Wrote: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.Mike Parker Wrote:Nobody has any ideas?okibi wrote: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.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?
May 01 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.
May 02 2007