CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2022
    Posts
    1

    Array of multiple types

    Hello guys,
    ive written this code:
    Code:
    class boundList
    {
    
        struct node{ void *elem{nullptr}; node *next{nullptr}; };
    
        node anchor;
        node *_Last;
    public:
        template<typename node1_t>
        boundList(node1_t *f_elem)
        {
            anchor.elem = f_elem;
            _Last = &anchor;
        }
        template<typename _Item>
        void append(_Item* n_elem)
        {
            _Last->next = new node{n_elem};
            _Last = _Last->next;
        }
        template<typename _OutType>
        _OutType get(const int index)
        {
            int i = 0;
    
            node *tmp = &anchor;
    
            while (i < index)
            {
                tmp = tmp->next;
                i++;
            }
            return *static_cast<_OutType*>(tmp->elem);
        }
         ~boundList()
         {
             node *tmp = anchor.next;
             node *next;
             while (tmp != _Last)
             {
                 next = tmp->next;
                 delete tmp;
                 tmp = next;
             }
         }
    };
    to create a class that takes any types and puts them together by storing the next pointer and so on. I then realized i have to convert them to void * to be able to do so. Now, if any object is requestes by the user, he has to provide the index AND its type to convert back from void*.
    how can I get it to only have to pass the index and have boudList figure out the type itself and return the correct type?
    Ive seen that std::tuble is capable of doing so, so there must be a possibility?!

    Thanks in advance

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

    Re: Array of multiple types

    [asked and answered here https://cplusplus.com/forum/general/285066/ ]
    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.4.4)

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