Sign in to follow this  

JavaScript literal object as C++ struct?

This topic is 1505 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Edited by mychii

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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. :)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites

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.

Edited by mychii

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites

I was talking about pure interface classes really, those have no data and all the methods are public (and pure virtual).

 

There's a Microsoft header for COM components that has

 

#define interface struct

 

anyway, for people who don't like the brevity of struct. LOL.

Share this post


Link to post
Share on other sites

 


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.

 

 

Hmm I guess you're right. biggrin.png

 

I was talking about pure interface classes really, those have no data and all the methods are public (and pure virtual).

 

There's a Microsoft header for COM components that has

 

#define interface struct

 

anyway, for people who don't like the brevity of struct. LOL.

 

I get you, and yeah it's just me who are not letting struct too much in the way laugh.png. Since it's much like a choice, I prefer class for now for pure interface. But I always keep your thought in mind just in case in time I let struct to be a little in my inner circle. Lol. tongue.png The good thing is at least I know now that struct could also be done the same as interface using class.

Edited by mychii

Share this post


Link to post
Share on other sites

This topic is 1505 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this