Constructor problem

Started by
21 comments, last by pdstatha 22 years, 4 months ago
In Java, boolean, byte, char, short, int, & long all go on the stack fine, but cannot go directly in the heap.
byte b;            // byte on stackint i = 80;        // int on stacklong l = new long; // Error! 

Everything else extends Object and goes only in the heap. Refrences to these objects (read: pointers) go on the stack.
String s1;                // Refrence to a string (null)String s2 = new String(); // Refrence pointing to String in heapString s3("Hi");          // Error! 


"So crucify the ego, before it''s far too late. To leave behind this place so negative and blind and cynical, and you will come to find that we are all one mind. Capable of all that''s imagined and all conceivable."
- Tool
------------------------------
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
Advertisement
Ah, so then he did need all of those news. But not in C or C++.
quote:Original post by merlin9x9
Ah, so then he did need all of those new s. But not in C or C++.


More precisely, in C/C++, when you declare a variable, it also has space allocated for it right away(on the stack), so if there is only one instance, then new is automatically called.

ie. MyClass test;

This allocates the memory and creates a new instance of MyClass.

In Java MyClass test; only declares the variable test is there, but sets it to null until space is specifically allocated for it by the programmer. You can do this right away or later.

ie. MyClass test=new MyClass();

or MyClass test;
.
.
.
test=new MyClass();

---
Make it work.
Make it fast.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Are you telling me that variable declaration results in a call to new? If so, that''s absolutely not true. Such variables have the implied auto keyword, which, as you said, results in stack-based allocation. The new operator is solely for dynamic allocation (on the heap or "free store") and is only called when you explicitly call it, just like you explicitly call malloc for dynamic allocation in C. Perhaps you were thinking of the constructor. If so, yes, the constructor gets called for both stack-allocated and dynamically-allocated (with new) objects.
I think were almost there, this is a real opener and very informative thanks once again. There seems to be only a couple of things the compiler doesn't like now.

        #include <iostream.h>#include <string>#include "classes.h"using namespace std;Tray *tray_prod_init() {	Tray *t_array = new Tray[10];//'Tray' : no appropriate default constructor available                                     //I'm presuming that I should use a similar def constructor to                                     //the one we used for the product???    //Set up List of Products    Product mars("Mars",40);    Product Snickers("Snickers",40);    Product Wispa("Wispa",35);    Product Aero("Aero",40);    Product Yorki("Yorki",40);    Product Galaxy("Galaxy",40);      And the second half          //Set up List of trays    Tray A1("A1",mars);    Tray A2("A2",Snickers);    Tray A3("A3",Wispa);    Tray A4("A4",Aero);    Tray A5("A5",Yorki);    Tray A6("A6",Galaxy);        //Assign position in array for each tray    Tray[0] = mars;//syntax error : missing ';' before '['    Tray[1
] = Snickers;//syntax error : missing ';' before '['

Tray[2] = Wispa;//syntax error : missing ';' before '['

Tray[3] = Aero;//syntax error : missing ';' before '['

Tray[4] = Yorki;//syntax error : missing ';' before '['

Tray[5] = Galaxy;//syntax error : missing ';' before '['


return t_array;

}

void main() {

Tray* tList = tray_prod_init(); // use tList

delete[] tList; // MAKE SURE YOU DO THIS WHEN YOU'RE DONE!!!


}
Edited by - pdstatha on January 3, 2002 8:36:11 AM

Edited by - pdstatha on January 3, 2002 8:38:04 AM

The text is there, just for some reason it's appeared in white so if you want to see it just highlight it by dragging your mouse.

Edited by - pdstatha on January 3, 2002 8:40:16 AM
quote:Original post by pdstatha
Tray *t_array = new Tray[10];//'Tray' : no appropriate default constructor available
//I'm presuming that I should use a similar def constructor to
//the one we used for the product???

Yes.
quote:Original post by pdstatha

//Assign position in array for each tray

Tray[0] = mars;//syntax error : missing ';' before '['
Tray[1] = Snickers;//syntax error : missing ';' before '['
Tray[2] = Wispa;//syntax error : missing ';' before '['
Tray[3] = Aero;//syntax error : missing ';' before '['
Tray[4] = Yorki;//syntax error : missing ';' before '['
Tray[5] = Galaxy;//syntax error : missing ';' before '['

return t_array;


A class is no different than an int or a float. If you used:
int *a=new int[10];
would you then use:
int[0]=5;
int[1]=8; .....

Replace Tray[0] with t_array[0] and you can figure out the rest.

---
Make it work.
Make it fast.

Edited by - CaptainJester on January 3, 2002 8:45:12 AM
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
LOL, shit yeah stupid me updated version of that bit:

      Tray A1("A1",mars);    Tray A2("A2",Snickers);    Tray A3("A3",Wispa);    Tray A4("A4",Aero);    Tray A5("A5",Yorki);    Tray A6("A6",Galaxy);        //Assign position in array for each tray    t_array[0] =  A1;    t_array[1] =  A2;    t_array[2] =  A3;    t_array[3] =  A4;    t_array[4] =  A5;    t_array[5] =  A6;  


I''ve also written a default constructor for the tray:

  Tray(const char* itsCode = " ", const Product* trayItem):		  code(itsCode), prod(trayItem)		  {}  


I think it''s almost there the only problem being is what would be the default parameter for "const Product* trayItem" be???
*bump*
ok here''s the issue:

you have a variable of type Product in your tray class and there is no default constructor for Product. That is ok ONLY IF you initialize Product in every tray constructor. If you don''t before you get around to initializing Product it will be in an invalid state and C++ does not allow that. So use one of those colons and put an initialization after.

The default value for a pointer is null but why are you using one anyway? Earlier you wanted to have a string and int in the constructor. Just make default parameters for those and it should work.
either that, or make a constructor with no parameters:
  Product::Product(void)  {  name = new string("NULL PRoduct");  price = 0;    };   

note: i don't use STL strings, so that is just a guess at how to create and initialize one.

--- krez (krezisback@aol.com)

EDIT: stupid CODE tag doesn't disable stupid smileys

Edited by - krez on January 3, 2002 2:21:48 PM
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])

This topic is closed to new replies.

Advertisement