Generics and toString methods? how do I make a toString() method work with unspecified types?
-
So I'm trying to create a generic ArrayStack ( that is to say an array-stack that can hold any type of data without having to specify etc ) I have a few questions, but let me give an example to illustrate my point: Say I've finished my ArrayStack and I say: ArrayStack myStack = new ArrayStack(); myStack.push ( X ) myStack.push ( Y ) myStack.push ( Z ) myStack.toString(); // now lets assume for a second X, Y and Z are instances of the Vector class. 1) Should my toString method just print out X, Y and Z or should it print out the contents of these vectors? 2)If I just said; string = string + X; Would the name just be added on automatically, or would I get a type-conflict? If so, how would I get around this? 3) Or would X's toString() method be called? if not, How would I check for/access the toString() method of an object passed through a constructor? 4) And finally, what about fundamental data types. How do I convert ints, doubles, longs, chars etc to Strings on the fly, as they're passed into my ArrayStack class? I'm sorry if this is a bit difficult to follow as a question, i'm just trying to get to grips with generics and all the possibilities involved =P thanks so much for your answers if you do manage to decipher my question :)
-
Answer:
Actually...it's that you're making it out to be 'difficult'. It's very simple really. If you're accepting input from a User where they have to type the input, you're can just accept everything as a string. string userInput = ""; cin >> userInput; From there you can do tests on userInput to see what the person entered. There are C-functions already defined that check to see if a piece of data 'is numeric'. You pass the userInput to it and it will tell you if it is or not. If it is numeric, just convert it to a type double if you need to use it for some math purposes. If you need to convert the double down to an int variable...just do this... int MyIntVariable; MyIntVariable = int ( TheDoubleValue );
Johnny at Yahoo! Answers Visit the source
Related Q & A:
- How do I make a window in Java?Best solution by Stack Overflow
- How can I make a Spinner only by java?Best solution by stackoverflow.com
- How do I make a python web program that is on a ubuntu server allow access to the server?Best solution by Yahoo! Answers
- How can I make a download page?Best solution by Stack Overflow
- How do I make my VCR/DVD work with my TiVo?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.