Solved

Array of Type

Posted on 2009-07-07
9
391 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

730 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