-
December 11th, 2013, 06:08 AM
#1
What is the equivalence to List<Object> in Java for C++?
Do I use std::vector<void *>?
Thanks
Jack
-
December 11th, 2013, 07:09 AM
#2
Re: What is the equivalence to List<Object> in Java for C++?
No, you change your design such that you don't need a god-object.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
December 11th, 2013, 08:44 AM
#3
Re: What is the equivalence to List<Object> in Java for C++?
The equivalence is std::list<myBaseObjectClass&>
but there is probably a better design approach altogether.
-
December 15th, 2013, 01:52 PM
#4
Re: What is the equivalence to List<Object> in Java for C++?
I better off redesign my program. Let me have a good thought about it
Thanks
Jack
-
December 17th, 2013, 05:46 PM
#5
Re: What is the equivalence to List<Object> in Java for C++?
Originally Posted by lucky6969b
Do I use std::vector<void *>?
List in Java is an interface. The two most common concrete implementations of List are ArrayList and LinkedList. The first corresponds with std::vector and the second with std::list.
The notion of a top class doesn't exist in C++ but one could think of void being such a class and then void* is pretty close to a reference to Object in Java (although void doesn't have any methods).
So std::vector<void*> of C++ corresponds very well with ArrayList<Object> of Java.
Last edited by razzle; December 17th, 2013 at 05:49 PM.
-
December 17th, 2013, 06:10 PM
#6
Re: What is the equivalence to List<Object> in Java for C++?
Anyone writing code that uses std::vector<void*> (or std::list<void*> or anything like this) that comes across my desk had better keep walking towards the exit.
As others have correctly pointed out, this is a design problem which needs to be re-thought.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 17th, 2013, 07:32 PM
#7
Re: What is the equivalence to List<Object> in Java for C++?
Originally Posted by 2kaud
that comes across my desk
Do they have desks at McDonalds now? Is it to raise the status of burger flipping? Just kidding, but you're jumping to conclusions. Did you see a design? No? So how can it be bad?
There's nothing inherently wrong with std::vector<void*>. That, or something similar, is what you can expect to find in low level C++ designs when you can't or don't want to make assumptions about type.
Maybe the OP realized he couldn't access hardware in Java and is now looking to do it in C++. And up pops a bunch of besserwissers yelling bad design. Lucky you to have a desk to hide your red face under.
Last edited by razzle; December 18th, 2013 at 04:15 AM.
-
December 18th, 2013, 12:36 AM
#8
Re: What is the equivalence to List<Object> in Java for C++?
Originally Posted by razzle
Is it to raise the status of burg
There's nothing inherently wrong with std::vector<void*>
Nothing wrong with it other than turning something most likely caught by a compiler into a runtime error. Unless you're used to program C with classes, you can restructure to get rid of the void*.
-
December 18th, 2013, 03:42 AM
#9
Re: What is the equivalence to List<Object> in Java for C++?
Originally Posted by Arjay
Nothing wrong with it other than turning something most likely caught by a compiler into a runtime error. Unless you're used to program C with classes, you can restructure to get rid of the void*.
And sometimes even if you're used to object oriented C++ no amount of restructuring can factor out a downcast. You simply have to deal with "a pointer to an unknown type" (the C++ Programming Language 4'th ed. by Stroustrup p. 172).
Lets face it, C++ is a versatile language and one of its strengts is that it allows you to program without contraceptives (at your own discression). Otherwise why don't we all switch to Java?
-
December 18th, 2013, 03:49 AM
#10
Re: What is the equivalence to List<Object> in Java for C++?
--- doubleposting by mistake ---
Last edited by razzle; December 18th, 2013 at 03:51 AM.
-
December 18th, 2013, 05:21 AM
#11
Re: What is the equivalence to List<Object> in Java for C++?
I agree that use of a void* pointer is sometimes useful and sometimes cannot be avoided. The point I was making clumsily (after a bad day ) was that I wouldn't expect to see containers/arrays of this type.
PS McDonalds do a good medium-rare flame war - and no, I couldn't hide under the desk as there's too much kit there.
Last edited by 2kaud; December 18th, 2013 at 05:24 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 18th, 2013, 12:24 PM
#12
Re: What is the equivalence to List<Object> in Java for C++?
Originally Posted by razzle
And sometimes even if you're used to object oriented C++ no amount of restructuring can factor out a downcast. You simply have to deal with "a pointer to an unknown type" (the C++ Programming Language 4'th ed. by Stroustrup p. 172).
Sure, there are always rare cases, but most times you need a pointer to an unknown type about as often as you need a goto. For some, that's more frequently than others.
-
December 19th, 2013, 03:49 AM
#13
Re: What is the equivalence to List<Object> in Java for C++?
Originally Posted by Arjay
Sure, there are always rare cases, but most times you need a pointer to an unknown type about as often as you need a goto. For some, that's more frequently than others.
You're allowing yourself a superior and condescending tone.
If you're such a genious why don't you enlighten us mere mortals as to why anyone should select C++ over Java if it isn't for the versatility of the former (and legacy is no issue)?
And does your oppsition to goto also include break in loops and return not at the end of functions? Should these also be used as rarely as void* in your view?
And while you're at it. What would you suggest to better correspond to ArrayList<Object> than std::vector<void*> ?
Last edited by razzle; December 19th, 2013 at 05:13 AM.
-
December 19th, 2013, 04:28 PM
#14
Re: What is the equivalence to List<Object> in Java for C++?
Originally Posted by razzle
You're allowing yourself a superior and condescending tone.
I only use that tone when working at McDonald's - a nod toward your previous superior and condescending comment:
Do they have desks at McDonalds now? Is it to raise the status of burger flipping?
Btw, ever notice the irony of folks that condescendingly refer to others as geniuses often misspell the word when doing so?
-
December 20th, 2013, 09:29 AM
#15
Re: What is the equivalence to List<Object> in Java for C++?
Originally Posted by razzle
So std::vector<void*> of C++ corresponds very well with ArrayList<Object> of Java.
It does, except that primitive types in java do not derive from Object, and would have to be wrapped.
If void* was God, then Object would be no better than Jesus I guess.
Nobody cares how it works as long as it works
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|