Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Creating array/object at runtime.

Creating array/object at runtime.

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structurestutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sameer_Thakur
    wrote on last edited by
    #1

    Hi. I want to know how I can create an array/object at runtime if I don’t know about its data type. For example, I have an array Say Arr, but I don’t have any information about the data type of elements it stores. It might be an array of int, float, char or even user defined data type (user defined class). I can get the information about its class or data type by using ‘type_info’, but how can I create an array/object of same data type at runtime? Like if I want to copy the contents of an array Arr into another array say ‘NewArr’; of the same type at runtime, how can I create that second array NewArr, i.e. what should I specify as a data type of ‘NewArr’?

    Thanks Sameer Thakur

    D M 2 Replies Last reply
    0
    • S Sameer_Thakur

      Hi. I want to know how I can create an array/object at runtime if I don’t know about its data type. For example, I have an array Say Arr, but I don’t have any information about the data type of elements it stores. It might be an array of int, float, char or even user defined data type (user defined class). I can get the information about its class or data type by using ‘type_info’, but how can I create an array/object of same data type at runtime? Like if I want to copy the contents of an array Arr into another array say ‘NewArr’; of the same type at runtime, how can I create that second array NewArr, i.e. what should I specify as a data type of ‘NewArr’?

      Thanks Sameer Thakur

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Sounds like a job for a template.


      "A good athlete is the result of a good and worthy opponent." - David Crow

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      1 Reply Last reply
      0
      • S Sameer_Thakur

        Hi. I want to know how I can create an array/object at runtime if I don’t know about its data type. For example, I have an array Say Arr, but I don’t have any information about the data type of elements it stores. It might be an array of int, float, char or even user defined data type (user defined class). I can get the information about its class or data type by using ‘type_info’, but how can I create an array/object of same data type at runtime? Like if I want to copy the contents of an array Arr into another array say ‘NewArr’; of the same type at runtime, how can I create that second array NewArr, i.e. what should I specify as a data type of ‘NewArr’?

        Thanks Sameer Thakur

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        Sameer_Thakur wrote:

        how can I create an array/object of same data type at runtime?

        AFAIK, there's no way to do this directly in C++. You can, however, design a system of classes that are capable of creating/cloning themselves at runtime and/or being created by type. For an example implementation, check out the source code for MFC's CRuntimeClass and CObject classes and the DECLARE_DYNAMIC/IMPLEMENT_DYNAMIC macros. This implementation has a common base class which includes methods for runtime object creation based on a class name. Because C++ has no built-in way to do this, the CRuntimeClass implements a list that maps class names to creator functions. Depending on the source of your source arrays, serialization may be useful as well. Regardless, at the heart of these solutions is code that creates objects of a specific class, which is all that C++ allows. Just my 2-cents, Mark

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups