Results 1 to 8 of 8

Thread: dynamic array of type

  1. #1

    Thread Starter
    Lively Member FantastichenEin's Avatar
    Join Date
    Mar 2000
    Location
    dairy
    Posts
    106

    dynamic array of type

    Code:
    Private Type tData
        Label As String
        Data As String
    End Type
    
    
    Private Sub Form_Load()
        Dim arrData() As tData
    End Sub
    I want to do the same as above in c++ (i.e. a dynamic array that holds a struct). I realise I'll be using a struct and vector but I cannot work out the implementation.

    Please can somebody give me a few ideas...

    Many thanks
    ****

  2. #2
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    struct
    {
    int *someInts;
    char *someChars;
    long count;
    }myStruct;

    .....
    // Some code
    myStruct *theStruct = new myStruct[872893];
    // Some more code

    This is for dynamic arrays.
    For vectors I think:

    typedef vector<myStruct> myVector;

    myVector haha;

    haha.push_back(...)

    And so on. It's the struct and typedef that matter. Declare the struct - or class - or whatever - then declare the vector using a typedef:

    typedef vector<Type> VectorTypeName;

    There you go. If my stuff is bad programming, don't worry as structs and vectors are detailed in the MSDN library.

    OK?

    HD

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    For Label and Data, I'd use the string class rather than a char* pointer.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4

    Thread Starter
    Lively Member FantastichenEin's Avatar
    Join Date
    Mar 2000
    Location
    dairy
    Posts
    106

    Post

    Thanks for the replies.

    I am still having trouble though:

    I have the following class:
    Code:
    #ifndef __CCONFIG_H__
    #define __CCONFIG_H__
    
    #include "CFile.h"
    #include <vector>
    
    
    class CConfig
    {
    public:
    	int Parse(const std::string &path);
    	struct CONFIG {std::string Label; std::string Value;};
    
    private:
    	std::vector<CONFIG> config();
    };
    
    
    #endif //__CCONFIG_H__
    I need to know how I can access this from the class implmenatation to add elements to the vector.

    Something like this? this->config[1].Label = "Label1";

    many thanks...
    ****

  5. #5
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    I think that this:

    std::vector<CONFIG> config();

    needs to be changed to this:

    typedef std::vector<CONFIG> config;

    This defines that a vector containing elements of type CONFIG exists and has type config.

    You have to initialise this:

    config myConfigVector;

    Then to add items you can use:

    CONFIG thisConfig;
    myConfigVector.push_back(thisConfig);

    All this information on Vectors can be found in MSDN.

    HD

  6. #6

    Thread Starter
    Lively Member FantastichenEin's Avatar
    Join Date
    Mar 2000
    Location
    dairy
    Posts
    106
    many thanks guys. works like a dream.

    don't need any typedefs though...
    ****

  7. #7
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    No problem. I'm so used to typedefs from C that I automatically do them.

    HD

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You don't need the typedefs, but they shorten your code and make it more readable.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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