JavaScript literal object as C++ struct?

Started by
10 comments, last by mychii 10 years, 6 months ago

Hi,

I just want to ask about the use of struct in C++ for you guys. I've been in JavaScript for a while now, and I have literal object that simply looks like this as an example:


var employee = {
  name: "John",
  id: 4903,
  job: "Mechanic"
}

In JavaScript I often use this as data representation only, instead of being able to do something. Does this actually works the same if I use C++ struct?

I saw C++ struct is much more flexible than the above, so if you can also share me many real-world possibilities on using struct (especially for games) it'll be very useful.

Thanks!

Advertisement

This is the closest you can do in C++:


static struct { // Don't ask about `static' here; just ignore it. ;)
  std::string name;
  unsigned id;
  std::string job;
} employee = {"John", 4903, "Mechanic"};

However, you would normally define a type `Employee' of which `employee' is an instance:


struct Employee {
  std::string name;
  unsigned id;
  std::string job;
};
 
Employee employee = {"John", 4903, "Mechanic"};

Depending on how you are going to use the type `Employee', it might be a good idea to provide a constructor:


struct Employee {
  std::string name;
  unsigned id;
  std::string job;
 
  Employee(std::string name, unsigned id, std::string job)
  : name(name), id(id), job(job) {
  }
};
 
Employee employee("John", 4903, "Mechanic");

But I am not entirely sure what your question is...

Thanks Alvaro actually that's also what I'm looking for.

In extension to that, I wonder what else I can use struct in game development other than for that conceptually? Just so I can come up with better ideas when to use struct.

Technically, C++ structs are almost identical to classes (with just a minor difference in default visibility). Realistically, C++ structs are typically used like C structs, or your Javascript Plain-Old-Data object above. Limiting it to that is actually pretty valuable.

Technically, C++ structs are almost identical to classes (with just a minor difference in default visibility). Realistically, C++ structs are typically used like C structs, or your Javascript Plain-Old-Data object above. Limiting it to that is actually pretty valuable.

Cool Thanks! Now I'm more confident to limit my struct usage to just POD. :)

In C++? Why don't you make pure virtual interface classes structs too, since they have no data and only specify a public interface.

(flame bait!). That's what Microsoft do though, so I'm claiming prior use if they try and trademark it.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

In C++? Why don't you make pure virtual interface classes structs too, since they have no data and only specify a public interface.

(flame bait!). That's what Microsoft do though, so I'm claiming prior use if they try and trademark it.

I suppose you could. Personally, I like to use the two keywords to separate objects with reference and value semantics (and personally, my value-semantics objects are always POD) like every other language in the world that actually uses both terms (AFAIK, at least).

Yep, but there's no enforcement in C++ so it just depends whether you like typing or not.

I kinda like the flexibility of C++ (but not the undefined behaviour).

If everything is public, why not make it a struct? interface would probably be a better keyword though to enforce no data and only public methods.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Yeah It's so flexible that it has so much choice, but as for interface I think I'll stick with class for now, but I surely keep that in mind for consideration since interface does have everything public.

If everything is public, why not make it a struct? interface would probably be a better keyword though to enforce no data and only public methods.

I'm not really sure though if the class is all public, but if it is conceptually an object and not just some sort of POD stuff, I think I don't want to take a risk if that class scales up and suddenly it needs some privacy.


I'm not really sure though if the class is all public, but if it is conceptually an object and not just some sort of POD stuff, I think I don't want to take a risk if that class scales up and suddenly it needs some privacy.

What risk? If you need privacy, you can add a `private:' section, or you can turn the struct into a class, and nothing bad will happen.

This topic is closed to new replies.

Advertisement