digitalmars.D - Factory Method
- Klaus Friedel (22/22) Sep 19 2007 Im sure I missed something. I tried to create a factory class producing ...
- Bill Baxter (21/49) Sep 19 2007 Its more like Java than you were thinking. :-) With 'class' objects in D...
- Nathan Reed (14/38) Sep 19 2007 In D, classes are reference types. This means when you declare a
- Klaus Friedel (5/55) Sep 19 2007 Thanks for the fast reply. I was sure I missed something ;-)
- Bill Baxter (13/72) Sep 19 2007 Yeh, that is ambiguous.
- Klaus Friedel (2/78) Sep 19 2007 New Bugzilla issue 1521 created.
- Kyle Furlong (4/81) Sep 19 2007 How many times are we going to have threads on this, by my count this is...
- Bill Baxter (5/97) Sep 19 2007 I was aware of the previous mentions of this problem in the docs. But
- janderson (4/6) Sep 19 2007 If you click on comment for the page you can add suggestions to a wiki
- janderson (6/16) Sep 19 2007 Page design wise I think those buttons up the top somehow go un-noticed....
Im sure I missed something. I tried to create a factory class producing objects on the heap like I would in C++ or Java: //************************************** class Cat{ int id; } class CatFactory{ Cat * createCat(){ Cat aCat = new Cat(); return &aCat; } } void test(){ CatFactory factory = new CatFactory(); Cat *c1 = factory.create(); Cat *c2 = factory.create(); } //************************************************************** I discoverd this would not work because aCat will be allocated on the stack instead the heap. Why ???? Looks anything but intuitive to me. What would be the correct way to create a object on the GC controlled heap an return a reference to that object ? Regards, Klaus Friedel
Sep 19 2007
Klaus Friedel wrote:Im sure I missed something. I tried to create a factory class producing objects on the heap like I would in C++ or Java: //************************************** class Cat{ int id; } class CatFactory{ Cat * createCat(){ Cat aCat = new Cat(); return &aCat; } } void test(){ CatFactory factory = new CatFactory(); Cat *c1 = factory.create(); Cat *c2 = factory.create(); } //************************************************************** I discoverd this would not work because aCat will be allocated on the stack instead the heap. Why ???? Looks anything but intuitive to me. What would be the correct way to create a object on the GC controlled heap an return a reference to that object ? Regards, Klaus FriedelIts more like Java than you were thinking. :-) With 'class' objects in D the pointer is implicit. aCat = new Cat() *is* a reference. // correct version class Cat{ int id; } class CatFactory{ Cat createCat(){ Cat aCat = new Cat(); return aCat; } } void test(){ CatFactory factory = new CatFactory(); Cat c1 = factory.create(); Cat c2 = factory.create(); } structs aren't implicitly references though. So to make a 'struct' factory you would need to have the pointer stuff. --bb
Sep 19 2007
Klaus Friedel wrote:Im sure I missed something. I tried to create a factory class producing objects on the heap like I would in C++ or Java: //************************************** class Cat{ int id; } class CatFactory{ Cat * createCat(){ Cat aCat = new Cat(); return &aCat; } } void test(){ CatFactory factory = new CatFactory(); Cat *c1 = factory.create(); Cat *c2 = factory.create(); } //************************************************************** I discoverd this would not work because aCat will be allocated on the stack instead the heap. Why ???? Looks anything but intuitive to me. What would be the correct way to create a object on the GC controlled heap an return a reference to that object ?In D, classes are reference types. This means when you declare a variable like "Cat aCat", that is really a reference to a Cat, and is allocated on the heap, not the stack. So, the correct way to do this would be: class CatFactor { Cat createCat () { return new Cat; } } The function allocates a Cat and returns a reference to it. Pointers are not necessary. Thanks, Nathan Reed
Sep 19 2007
Nathan Reed Wrote:Klaus Friedel wrote:Thanks for the fast reply. I was sure I missed something ;-) What really confused me was the following statement under "Memory Management" http://www.digitalmars.com/d/memory.html#stackclass :Im sure I missed something. I tried to create a factory class producing objects on the heap like I would in C++ or Java: //************************************** class Cat{ int id; } class CatFactory{ Cat * createCat(){ Cat aCat = new Cat(); return &aCat; } } void test(){ CatFactory factory = new CatFactory(); Cat *c1 = factory.create(); Cat *c2 = factory.create(); } //************************************************************** I discoverd this would not work because aCat will be allocated on the stack instead the heap. Why ???? Looks anything but intuitive to me. What would be the correct way to create a object on the GC controlled heap an return a reference to that object ?In D, classes are reference types. This means when you declare a variable like "Cat aCat", that is really a reference to a Cat, and is allocated on the heap, not the stack. So, the correct way to do this would be: class CatFactor { Cat createCat () { return new Cat; } } The function allocates a Cat and returns a reference to it. Pointers are not necessary. Thanks, Nathan ReedI thought of the "*" as "or" but after rereading I now understand it ment "and". Maybe somebody should edit this chapter to prevent others from making the same mistake ?Class instances are normally allocated on the garbage collected heap. However, if they: * are allocated as local symbols in a function * are allocated using new * use new with no arguments * have the scope storage class then they are allocated on the stack. This is more efficient than doing an allocate/free >>>cycle on the instance. But be careful that any reference to the object does not survive the return of the function.
Sep 19 2007
Klaus Friedel wrote:Nathan Reed Wrote:Yeh, that is ambiguous. Care to file a doc bug on it? I think inserting one 'and' is all that's needed: """ * are allocated as local symbols in a function * are allocated using new * use new with no arguments * and have the scope storage class """ Bugzilla is here: http://d.puremagic.com/issues/ --bbKlaus Friedel wrote:Thanks for the fast reply. I was sure I missed something ;-) What really confused me was the following statement under "Memory Management" http://www.digitalmars.com/d/memory.html#stackclass :Im sure I missed something. I tried to create a factory class producing objects on the heap like I would in C++ or Java: //************************************** class Cat{ int id; } class CatFactory{ Cat * createCat(){ Cat aCat = new Cat(); return &aCat; } } void test(){ CatFactory factory = new CatFactory(); Cat *c1 = factory.create(); Cat *c2 = factory.create(); } //************************************************************** I discoverd this would not work because aCat will be allocated on the stack instead the heap. Why ???? Looks anything but intuitive to me. What would be the correct way to create a object on the GC controlled heap an return a reference to that object ?In D, classes are reference types. This means when you declare a variable like "Cat aCat", that is really a reference to a Cat, and is allocated on the heap, not the stack. So, the correct way to do this would be: class CatFactor { Cat createCat () { return new Cat; } } The function allocates a Cat and returns a reference to it. Pointers are not necessary. Thanks, Nathan ReedI thought of the "*" as "or" but after rereading I now understand it ment "and". Maybe somebody should edit this chapter to prevent others from making the same mistake ?Class instances are normally allocated on the garbage collected heap. However, if they: * are allocated as local symbols in a function * are allocated using new * use new with no arguments * have the scope storage class then they are allocated on the stack. This is more efficient than doing an allocate/free >>>cycle on the instance. But be careful that any reference to the object does not survive the return of the function.
Sep 19 2007
Bill Baxter Wrote:Klaus Friedel wrote:New Bugzilla issue 1521 created.Nathan Reed Wrote:Yeh, that is ambiguous. Care to file a doc bug on it? I think inserting one 'and' is all that's needed: """ * are allocated as local symbols in a function * are allocated using new * use new with no arguments * and have the scope storage class """ Bugzilla is here: http://d.puremagic.com/issues/ --bbKlaus Friedel wrote:Thanks for the fast reply. I was sure I missed something ;-) What really confused me was the following statement under "Memory Management" http://www.digitalmars.com/d/memory.html#stackclass :Im sure I missed something. I tried to create a factory class producing objects on the heap like I would in C++ or Java: //************************************** class Cat{ int id; } class CatFactory{ Cat * createCat(){ Cat aCat = new Cat(); return &aCat; } } void test(){ CatFactory factory = new CatFactory(); Cat *c1 = factory.create(); Cat *c2 = factory.create(); } //************************************************************** I discoverd this would not work because aCat will be allocated on the stack instead the heap. Why ???? Looks anything but intuitive to me. What would be the correct way to create a object on the GC controlled heap an return a reference to that object ?In D, classes are reference types. This means when you declare a variable like "Cat aCat", that is really a reference to a Cat, and is allocated on the heap, not the stack. So, the correct way to do this would be: class CatFactor { Cat createCat () { return new Cat; } } The function allocates a Cat and returns a reference to it. Pointers are not necessary. Thanks, Nathan ReedI thought of the "*" as "or" but after rereading I now understand it ment "and". Maybe somebody should edit this chapter to prevent others from making the same mistake ?Class instances are normally allocated on the garbage collected heap. However, if they: * are allocated as local symbols in a function * are allocated using new * use new with no arguments * have the scope storage class then they are allocated on the stack. This is more efficient than doing an allocate/free >>>cycle on the instance. But be careful that any reference to the object does not survive the return of the function.
Sep 19 2007
Klaus Friedel wrote:Bill Baxter Wrote:How many times are we going to have threads on this, by my count this is the third time in the last few months this ambiguity has confused someone. Just fix the damn thing already.Klaus Friedel wrote:New Bugzilla issue 1521 created.Nathan Reed Wrote:Yeh, that is ambiguous. Care to file a doc bug on it? I think inserting one 'and' is all that's needed: """ * are allocated as local symbols in a function * are allocated using new * use new with no arguments * and have the scope storage class """ Bugzilla is here: http://d.puremagic.com/issues/ --bbKlaus Friedel wrote:Thanks for the fast reply. I was sure I missed something ;-) What really confused me was the following statement under "Memory Management" http://www.digitalmars.com/d/memory.html#stackclass :Im sure I missed something. I tried to create a factory class producing objects on the heap like I would in C++ or Java: //************************************** class Cat{ int id; } class CatFactory{ Cat * createCat(){ Cat aCat = new Cat(); return &aCat; } } void test(){ CatFactory factory = new CatFactory(); Cat *c1 = factory.create(); Cat *c2 = factory.create(); } //************************************************************** I discoverd this would not work because aCat will be allocated on the stack instead the heap. Why ???? Looks anything but intuitive to me. What would be the correct way to create a object on the GC controlled heap an return a reference to that object ?In D, classes are reference types. This means when you declare a variable like "Cat aCat", that is really a reference to a Cat, and is allocated on the heap, not the stack. So, the correct way to do this would be: class CatFactor { Cat createCat () { return new Cat; } } The function allocates a Cat and returns a reference to it. Pointers are not necessary. Thanks, Nathan ReedI thought of the "*" as "or" but after rereading I now understand it ment "and". Maybe somebody should edit this chapter to prevent others from making the same mistake ?Class instances are normally allocated on the garbage collected heap. However, if they: * are allocated as local symbols in a function * are allocated using new * use new with no arguments * have the scope storage class then they are allocated on the stack. This is more efficient than doing an allocate/free >>>cycle on the instance. But be careful that any reference to the object does not survive the return of the function.
Sep 19 2007
Kyle Furlong wrote:Klaus Friedel wrote:I was aware of the previous mentions of this problem in the docs. But Walter's usually good about making simple doc fixes as long as there's an actual bug filed for it. And now there is, thanks to Klaus. --bbBill Baxter Wrote:How many times are we going to have threads on this, by my count this is the third time in the last few months this ambiguity has confused someone. Just fix the damn thing already.Klaus Friedel wrote:New Bugzilla issue 1521 created.Nathan Reed Wrote:Yeh, that is ambiguous. Care to file a doc bug on it? I think inserting one 'and' is all that's needed: """ * are allocated as local symbols in a function * are allocated using new * use new with no arguments * and have the scope storage class """ Bugzilla is here: http://d.puremagic.com/issues/ --bbKlaus Friedel wrote:Thanks for the fast reply. I was sure I missed something ;-) What really confused me was the following statement under "Memory Management" http://www.digitalmars.com/d/memory.html#stackclass :Im sure I missed something. I tried to create a factory class producing objects on the heap like I would in C++ or Java: //************************************** class Cat{ int id; } class CatFactory{ Cat * createCat(){ Cat aCat = new Cat(); return &aCat; } } void test(){ CatFactory factory = new CatFactory(); Cat *c1 = factory.create(); Cat *c2 = factory.create(); } //************************************************************** I discoverd this would not work because aCat will be allocated on the stack instead the heap. Why ???? Looks anything but intuitive to me. What would be the correct way to create a object on the GC controlled heap an return a reference to that object ?In D, classes are reference types. This means when you declare a variable like "Cat aCat", that is really a reference to a Cat, and is allocated on the heap, not the stack. So, the correct way to do this would be: class CatFactor { Cat createCat () { return new Cat; } } The function allocates a Cat and returns a reference to it. Pointers are not necessary. Thanks, Nathan ReedI thought of the "*" as "or" but after rereading I now understand it ment "and". Maybe somebody should edit this chapter to prevent others from making the same mistake ?Class instances are normally allocated on the garbage collected heap. However, if they: * are allocated as local symbols in a function * are allocated using new * use new with no arguments * have the scope storage class then they are allocated on the stack. This is more efficient than doing an allocate/free >>>cycle on the instance. But be careful that any reference to the object does not survive the return of the function.
Sep 19 2007
Klaus Friedel wrote:I thought of the "*" as "or" but after rereading I now understand it ment "and". Maybe somebody should edit this chapter to prevent others from making the same mistake ?If you click on comment for the page you can add suggestions to a wiki page about improvements, and also see more information about the topic. -Joel
Sep 19 2007
janderson wrote:Klaus Friedel wrote:Page design wise I think those buttons up the top somehow go un-noticed. particularly comment. I think at the end of each page there should be a link like "Discussion this page on the wiki" or "Wiki entry for this page". -JoelI thought of the "*" as "or" but after rereading I now understand it ment "and". Maybe somebody should edit this chapter to prevent others from making the same mistake ?If you click on comment for the page you can add suggestions to a wiki page about improvements, and also see more information about the topic. -Joel
Sep 19 2007