Solved

Array of Type

Posted on 2009-07-07
9
387 Views
Last Modified: 2012-05-07
I would like to have an array like this:

Hypothetical PseudoCode

struct mystruct
{
      char* tag;
      T type;
};

Completely Wrong but want to set up a const array like this
  const SomeStruct[] = { ( "tag1", double), ("tag2", int), ("tag3", char*)};

 Any way to do this using templates?

 
0
Comment
Question by:williamcampbell
9 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 24799194
>> Any way to do this using templates?
Specifically what do you want to do with templates and why do you think that would give a better solution?

BTW: Template values can only be integral types (numeric integer values known at compile time but not real numbers like float or double) and whilst char * is a valid value parameter for a template you can't actually pass a "string" to instantiate it because the address of the string is not known until runtime.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 200 total points
ID: 24799223
A similar (although not to do with arrays but to do with template value parameters) that I assisted on today that you might find useful to read. {http:/Q_24546546.html}
0
 
LVL 12

Author Comment

by:williamcampbell
ID: 24799332
Interesting read

      is something like below possible?

      template< class T>
      struct item
      {
            item (std::sting i) { tag=i; }
            std::string tag;
      };

      somearray = { new item<int> ( "tag1"), new item<double> ( "tag2") }
0
 
LVL 12

Author Comment

by:williamcampbell
ID: 24799348
Heres the scenario:

  Coming over the wire will be random string tags and some data. Depending on what the tag name is, determine the type from a const array (or vector or whatever) and interpret the data.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 100 total points
ID: 24800963
I think what you want is a union inside the struct. Something like below.

You will of course have to define all types at compile time, but you can have an array of structs, whose contents can vary depending on the tag string.

Note that instead of a string for the tag, it might be more appropriate to use an enum ...
struct MyStruct {

  std::string tag;

  union {

    int value_int;

    double value_double;

    // ...

  } value;

};
 

MyStruct someArray[2];

someArray[0].tag = "tag1";

someArray[0].value_int = 5;

someArray[1].tag = "tag1";

someArray[1].value_double = 10.0;

Open in new window

0
 
LVL 12

Assisted Solution

by:Let_Me_Be
Let_Me_Be earned 100 total points
ID: 24801182
Will be coming in C++0x (check your compiler for support, it might already be there).

http://en.wikipedia.org/wiki/C%2B%2B0x#Initializer_lists
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24801221
>> check your compiler for support, it might already be there
Any compiler that is TR1 ready will (should?) support this... but remember it is NOT portable until it is ratified.


Can't you just use a std::map? The key being the string tag and the value being whatever you need it to resolve to (a struct of const data or a polymorphic handler class for example)?

http://www.cplusplus.com/reference/stl/map/
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 100 total points
ID: 24802053
>>>> Can't you just use a std::map?

I recently had a project which has a dictionary similar to

class Dictionary : public Object
{
    std::map<std::string, Object*> > mymap;
public:
    // populating all map member functions
};

Then they had class types for all C types (POD) using

template <class  T>
class PodType : public Object
{
    ...
};
typedef PodType<int> IntType;
...

as well as for all possible container types what made these types storable as Object pointers.

That way a Dictionary or Array could take any type as value type, including dictionaries and arrays ... It supported full copy semantics.

The only disadvantage was that the objects retrieved from those containers must be casted to their real type before use as not all fucntionality could be made via virtual functions.
0
 
LVL 12

Author Closing Comment

by:williamcampbell
ID: 31600873
Thanks guys lots to think about
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now