Sign in to follow this  
munito

Modern C++ looking for information

Recommended Posts

munito    1186

Hey everyone,

 

after working for 5 years with Java in my current job and in my hobby projects I'm getting tired of writing all my stuff in this language.

I think the time has come to fulfill my dream and start diving deeper into the C universe.

 

First thing I want to try is to port one of my Java projects into a C++ project. What I am looking for is a guide for code styling, project structure and modern C++ usage.

 

I'm going to use Visual Studio 2013 and want to take a look at SDL or SFML for the start. Any good advice for a Java developer exploring the big world of C/C++ language?

 

I'd would be really grateful if someone can share his knowledge with me or post intresting links.

 

Best Regards

Olaf

Share this post


Link to post
Share on other sites
munito    1186

Thanks everyone for the resources, I'll gonna have a look at them!

 

I've already got some questions regarding some Java standards and how they are solved in C++.

 

1)

I'm totally used to create my Java classes in POJO manner. That means almost every member of my class will have a getter/setter method for accessing it. I've read that in C++ getters/setters are evil and break the rule of data encapsulation. In eclipse I've got a function to automatically create getters/setters but somehow in visual studio im missing that function. Any advice here?

 

2)

It feels like visual studio is missing alot features from eclipse. For example:

-Ctrl + O - Opens a context menu with all class members / functions listed (Outline)

-Ctrl + Alt + Arrow - Copies the current selected line

-Ctrl + 1 - Opens  the context help menu

...

Anyone knows a guide for visual studio hot keys and shortcuts?

 

Best Regards

Olaf

Share this post


Link to post
Share on other sites
ilreh    293

For 1) the equivalent of POJO is POD - where you make a struct where all the members are public, and all the members are either primitive types, raw pointers, or other POD types.

If every member has a getter/setter, then they may as well just be public.

Although they can be used equivalently there are differences. If you add a set-method you can include error-handling and polymorphism which can help you maintain correct behavior.

For example:
you have a class Player and a member called health. If you use a set-method you can also implement something like:
 

void Player::setHealth(int x) {
  if (x < (maxhealth /4) && x > 0) {
    Player::yell("I need a medic!");
  }
  if (x <= 0) {
    Player::setState(DEAD);
  }
  health = x;
}

Of course right now you would probably implement stuff like this in a method like "registerDamage" and this would probably make more sense but if you design a set-method from begin with you are more flexible to changes. You could also check for a negative number and add a line to your log that something went wrong.

 

Another advantage of using set-methods is that you can easily implement conceptional future changes. For example if you decide later on that max health should be 99 instead of originally planned 9999, you simply have to add a /100 here and it's all set. Otherwise you'd have to go through your code and modify every occurence where the variable was changed from the outside.

Edited by ilreh

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this