0

I have this code

enum type {NOTHING, SOMETHING, SOMETHINGELSE}
type *x;

At the moment I use x[765] == SOMETHING for example, How would I store other values for example

x[765] == SOMETHINGELSE;
x[765].position == 43.5;
x[765].somevar == 12;

I will apologize for my poor wording within my question im just starting out in C++, I know what I want i'm just not to sure on how to ask it.

Thanks.

Elgoog
  • 2,205
  • 7
  • 36
  • 48
  • 1
    `==` is the comparison operator. If you want the assignment operator then that is a single `=`. – Troubadour Oct 27 '11 at 20:13
  • 2
    If you're just starting out may I suggest one of the fine introductory books on the book list? http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Mark B Oct 27 '11 at 20:14
  • I realize this, its just for demo purposes to show an example value :) – Elgoog Oct 27 '11 at 20:15

5 Answers5

2

It looks as if you're looking for a way to structure 'knowledge'; this is done with a struct or a class:

#include <vector>

struct Info {
   enum thingness { nothing, something };

   // 'member' variables
   thingness howMuch;
   int a_counter;
   float position;
};

int main(){
  Info object;
  object.howMuch=Info::something;
  object.a_counter=1;
  object.position=5.4;

You can group these kinds of objects into a container - typically an std::vector:

  // a container of InterestingValues
  std::vector<Info> container(300);

  container[299].howMuch=Info::nothing;
  container[299].a_counter=4;
  container[299].position = 3.3;

  // or assign rightaway:
  container[2] = object;
}
xtofl
  • 40,723
  • 12
  • 105
  • 192
1

You will have to make yourself a more complex type:

struct type
{
    enum flag_type
    {
        NOTHING, SOMETHING, SOMETHINGELSE
    } flag;
    double position;
    int somevar;
};

and later have an array of this new type.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
1

Get yourself a good book to learn from. A list of good books is available here: The Definitive C++ Book Guide and List

In C++, you are asking how to declare an array of structures. Try this:

struct type {
    double position;
    int somevar;
};

type *x;
x[765].position = 43.5;
x[765].somevar = 12;
Community
  • 1
  • 1
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

An enum is a replaceable label basically for an int. You need to define a struct or a class.

struct type
{
   float position ;

};

type var;
var.position = 3.4;
rerun
  • 25,014
  • 6
  • 48
  • 78
0

Your type enum would need to be a member of a class, along with the other fields. For example,

class MyType
{
public:
    type t;
    double position;
    int somevar;
};

With an array of MyType instances

MyType *x;

you would then be able to do what you ask expect you would need to do

x[765].t = SOMETHINGELSE;

to assign to the enum.

Troubadour
  • 13,334
  • 2
  • 38
  • 57