Changing Class Variable Capitalization in Code::Blocks - c++

SO I've been using Code::Blocks for awhile now, and I've experienced this annoying thing for quite awhile now. When you're creating a new class, it has the following setup page:
In the "Add new" section, we can, of course, add new variables and include "Getter" and "Setter" methods. So we add a few variables, and create a new class:
But here's where the problem occurs; when the getter and setter methods are added, they are named by default as "Getname" and "Setname." My question is: is there anyway to change it to be, by default "getName" and "setName?" (Note the change of capitalization). Or, on the other hand, should I be changing my coding style to match that of Code::Blocks? Thank you for your answers!

looks like this is done on purpose
http://wiki.codeblocks.org/index.php/Coding_style

Related

C++ Evil Getter Setter against Factory/Builder design pattern

I'm trying to learn the design pattern norme (like refacturing guru), and i have some problem to understand how i could merge the idea of bad design with public getter/setter and factory/Builder that need "out of constructor" variable setter.
for example with the answer of this article to article to Design pattern
As you can understand, each object will need a lot of informations, and adding part should set the needed informations, but to be able to do it, it need accessor to my variable outside the constructor.
Help me figure out what i'm missing.
--- Edit
To be more precise, Let's say i have 2 class :
CombatObject <---- Spaceships
And i have a factory that will create different type of spaceships (principally because i don't want to create more than 10 class just to change the stats of the objects)
in this case, getter/setter are not a bad design (or are they?)
Ok i think the comments are right, the way i see the solution is the following :
No Setter, the only way to interact with the object will be with function with a purpose.
For exemple, modify HP, add a DoDamage function that will return false if the ship is destoyed and will internally modify the hp (and maybe the damage too, etc..)
Getter can be public, but of course, only "const &"
Clone method is a good idea for future development (prototype pattern)

Why not set class members to public in the first place? [duplicate]

This question already has answers here:
Why use getters and setters/accessors?
(37 answers)
Closed 9 years ago.
I can't understand why people keep saying that when you make a class you should make a get() method to get the variable in a class, and a set() method to set a variable in a class.
If you have a get()/set() method... then why not just make the variable public in the first place?
It's not as if you would accidentally change it anyway since you still need to type myClassName.variable
Am I missing something?
Using get/set functions gives you flexibility to change your class implementation later without having to change all code which accesses the class.
For example, you might change the type of one of your internal variables, or you want to add restrictions on what values a variable can be set to. Perhaps you might want to only calculate a value on demand.
Also, you can make a get/set function non-inline, and put a breakpoint on it.
Making public variables is a very bad practice(in OO), because if you want to add any functionality or restrictions you will have to find all the code that accesses your class and replace that.
Getters and setters solve the issue of having to change all that code, when you want do add a restriction to a value of a variable, to do a validation or another functionality.
But don't overuse get/set functions, getters and setters also break OO principles, because they are still exposing implementation. If you really want your code to be reusable on different projects, use get/set functions only when there's no other possible implementation.
If you don't believe me, read this old but still relevant articles, and take your own conclusions.
http://www.idinews.com/quasiClass.pdf
http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html
http://typicalprogrammer.com/?p=23

Mutual inclusion with singleton

I currently have a problem with mutual inclusion in a project of mine (C++).
Normally, if I had a mutual inclusion problem, I'd easily solve it by either forward declaration or redesigning the classes a bit. But this time, I'm stuck:
I have a class called Game, which creates and launches core game systems, where one of these is called GraphicsSystem.
In the Game constructor, a GraphicsSystem object is dynamically created and stored in a GraphicsSystem* pointer. Looks like this:
Game::Game ()
{
gfxSys = new GraphicsSystem(80, new Camera());
}
Now my GraphicsSystem class needs to access a Game class's method at one point to get the player object, which is stored within the Game object. The respective part looks like this (Game is btw a singleton!):
void GraphicsSystem::handleFrame ()
{
ElementList elements = Game::instance()->getPlayer()->getEnvironment()->getElements();
}
Now I tried forward declaration in both directions already, but that wouldn't satisfy the compiler. I could of course also just put the player pointer into the graphics system, but I really don't want to do that, since he doesn't belong there.
Is there any way to resolve this without me having to change the design too much? I'm really stumped right now, so I hope you guys can help me.
I'm using Visual Studio 2010 (Visual C++).
Thank you in advance!
Chris
Forward declare GraphicsSystem in Game.h. Include Game.h in GraphicsSystem.h if it needs it. Include Game.h and GraphicsSystem.h in Game.cpp. Include Game.h and GraphicsSystem.h in GraphicsSystem.cpp.
That should work based off the code you posted.
There are two main ways to break circular dependencies in C++.
1) Create a 'interface' class which lists the methods implemented by your real class and which your real class inherits from. Then you can include that instead of the declaration of the real class.
2) Pimpl - a class holds an opaque pointer to its real data, solving the problem of having to include declarations for the types of member variables.
I know this is going to sound dodgy, but have you considered holding a global reference to the initialized Game class?
I know globals are evil and so on and so forth, and I'm sure lots of people are going to rage, but in my personal experience I have succumb to Singletonitis with the Game class (among others) and found that sometimes it just simply isn't necessary.
The biggest question you need to ask yourself is "Who are you trying to protect your Game object from? and is there any reason why you need to request an instance each time you need the game class? I'm pretty sure that if your Game object wasn't initialized, you wouldn't be doing much else in your engine anyway.
Hope this might help you think about things in a different way, even though it's not answering your question directly.

Deleting a method from Visual Studio properties window

The "Events", "Messages" and "Overrides" tabs in the Properties Window can be used to add new methods to a class as well as to remove them. However, when you select to "Delete" a method, it comments the method code instead of deleting it.
I know this is for safety issues, but I almost never need the commented code and end up having to delete it manually. This is even more annoying in MFC, when I have to delete the method declaration, the method implementation and the entry on the message map which are usually on different places.
Is there an option to simply delete the method code instead of just commenting it?
No there is no way of doing this, especially in C++ where there are too many places that directly or indirectly exist as part of the method declaration.
Even if there was an option for this, I don't think I'd trust it to do the right thing.

Extending an existing class like a namespace (C++)?

I'm writing in second-person just because its easy, for you.
You are working with a game engine and really wish a particular engine class had a new method that does 'bla'. But you'd rather not spread your 'game' code into the 'engine' code.
So you could derive a new class from it with your one new method and put that code in your 'game' source directory, but maybe there's another option?
So this is probably completely illegal in the C++ language, but you thought at first, "perhaps I can add a new method to an existing class via my own header that includes the 'parent' header and some special syntax. This is possible when working with a namespace, for example..."
Assuming you can't declare methods of a class across multiple headers (and you are pretty darn sure you can't), what are the other options that support a clean divide between 'middleware/engine/library' and 'application', you wonder?
My only question to you is, "does your added functionality need to be a member function, or can it be a free function?" If what you want to do can be solved using the class's existing interface, then the only difference is the syntax, and you should use a free function (if you think that's "ugly", then... suck it up and move on, C++ wasn't designed for monkeypatching).
If you're trying to get at the internal guts of the class, it may be a sign that the original class is lacking in flexibility (it doesn't expose enough information for you to do what you want from the public interface). If that's the case, maybe the original class can be "completed", and you're back to putting a free function on top of it.
If absolutely none of that will work, and you just must have a member function (e.g. original class provided protected members you want to get at, and you don't have the freedom to modify the original interface)... only then resort to inheritance and member-function implementation.
For an in-depth discussion (and deconstruction of std::string'), check out this Guru of the Week "Monolith" class article.
Sounds like a 'acts upon' relationship, which would not fit in an inheritance (use sparingly!).
One option would be a composition utility class that acts upon a certain instance of the 'Engine' by being instantiated with a pointer to it.
Inheritance (as you pointed out), or
Use a function instead of a method, or
Alter the engine code itself, but isolate and manage the changes using a patch-manager like quilt or Mercurial/MQ
I don't see what's wrong with inheritance in this context though.
If the new method will be implemented using the existing public interface, then arguably it's more object oriented for it to be a separate function rather than a method. At least, Scott Meyers argues that it is.
Why? Because it gives better encapsulation. IIRC the argument goes that the class interface should define things that the object does. Helper-style functions are things that can be done with/to the object, not things that the object must do itself. So they don't belong in the class. If they are in the class, they can unnecessarily access private members and hence widen the hiding of that member and hence the number of lines of code that need to be touched if the private member changes in any way.
Of course if you want to access protected members then you must inherit. If your desired method requires per-instance state, but not access to protected members, then you can either inherit or composite according to taste - the former is usually more concise, but has certain disadvantages if the relationship isn't really "is a".
Sounds like you want Ruby mixins. Not sure there's anything close in C++. I think you have to do the inheritance.
Edit: You might be able to put a friend method in and use it like a mixin, but I think you'd start to break your encapsulation in a bad way.
You could do something COM-like, where the base class supports a QueryInterface() method which lets you ask for an interface that has that method on it. This is fairly trivial to implement in C++, you don't need COM per se.
You could also "pretend" to be a more dynamic language and have an array of callbacks as "methods" and gin up a way to call them using templates or macros and pushing 'this' onto the stack before the rest of the parameters. But it would be insane :)
Or Categories in Objective C.
There are conceptual approaches to extending class architectures (not single classes) in C++, but it's not a casual act, and requires planning ahead of time. Sorry.
Sounds like a classic inheritance problem to me. Except I would drop the code in an "Engine Enhancements" directory & include that concept in your architecture.