CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    What is the equivalence to List<Object> in Java for C++?

    Do I use std::vector<void *>?
    Thanks
    Jack

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    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

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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.

  4. #4
    Join Date
    Dec 2010
    Posts
    907

    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

  5. #5
    Join Date
    Jul 2013
    Posts
    576

    Re: What is the equivalence to List<Object> in Java for C++?

    Quote Originally Posted by lucky6969b View Post
    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.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  7. #7
    Join Date
    Jul 2013
    Posts
    576

    Re: What is the equivalence to List<Object> in Java for C++?

    Quote Originally Posted by 2kaud View Post
    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.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: What is the equivalence to List<Object> in Java for C++?

    Quote Originally Posted by razzle View Post
    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*.

  9. #9
    Join Date
    Jul 2013
    Posts
    576

    Re: What is the equivalence to List<Object> in Java for C++?

    Quote Originally Posted by Arjay View Post
    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?

  10. #10
    Join Date
    Jul 2013
    Posts
    576

    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.

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: What is the equivalence to List<Object> in Java for C++?

    Quote Originally Posted by razzle View Post
    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.

  13. #13
    Join Date
    Jul 2013
    Posts
    576

    Re: What is the equivalence to List<Object> in Java for C++?

    Quote Originally Posted by Arjay View Post
    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.

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: What is the equivalence to List<Object> in Java for C++?

    Quote Originally Posted by razzle View Post
    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?

  15. #15
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: What is the equivalence to List<Object> in Java for C++?

    Quote Originally Posted by razzle View Post
    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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured