Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Array of Type

Posted on 2009-07-07
9
Medium Priority
?
394 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 800 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
Industry Leaders: 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 400 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 400 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 400 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

618 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