Deleting a method from Visual Studio properties window - c++

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.

Related

Changing Class Variable Capitalization in Code::Blocks

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

Programming pattern for components that are toggleable at runtime

I'm wondering if there is some kind of logical programming pattern or structure that I should be using if sometimes during runtime a component should be used and other times not. The obvious simple solution is to just use if-else statements everywhere. I'm trying to avoid littering my code with if-else statements since once the component is toggled on, it will more than likely be on for a while and I wonder if its worth it to recheck if the same component is active all over the place when the answer will most likely not have changed between checks.
Thanks
A brief example of what I'm trying to avoid
class MainClass
{
public:
// constructors, destructors, etc
private:
ComponentClass m_TogglableComponent;
}
// somewhere else in the codebase
if (m_TogglableComponent.IsActive())
{
// do stuff
}
// somewhere totally different in the codebase
if (m_TogglableComponent.IsActive())
{
// do some different stuff
}
Looks like you're headed towards a feature toggle. This is a common occurrence when there's a piece of functionality that you need to be able to toggle on or off at run time. The key piece of insight with this approach is to use polymorphism instead of if/else statements, leveraging object oriented practices.
Martin Fowler details an approach here, as well as his rationale: http://martinfowler.com/articles/feature-toggles.html
But for a quick answer, instead of having state in your ComponentClass that tells observers whether it's active or not, you'll want to make a base class, AbstractComponentClass, and two base classes ActiveComponentClass and InactiveComponentClass. Bear in mind that m_TogglableComponent is currently an automatic member, and you'll need to make it a pointer under this new setup.
AbstractComponentClass will define pure virtual methods that both need to implement. In ActiveComponentClass you will put your normal functionality, as if it were enabled. In InactiveComponentClass you do as little as possible, enough to make the component invisible as far as MainClass is concerned. Void functions will do nothing and functions return values will return neutral values.
The last step is creating an instance of one of these two classes. This is where you bring in dependency injection. In your constructor to MainClass, you'll take a pointer of type AbstractComponentClass. From there on it doesn't care if it's Active or Inactive, it just calls the virtual functions. Whoever owns or controls MainClass is the one that injects the kind that you want, either active or inactive, which could be read by configuration or however else your system decides when to toggle.
If you need to change the behaviour at run time, you'll also need a setter method that takes another AbstractComponentClass pointer and replaces the one from the constructor.

Additional functions added to a class cause a segfault when class is created as a shared_ptr

This really has me stumped.
We have a class with a dozen or so getters and setters defined, that take a mix of types (QString, int, bool) for the various member variables. I'm currently adding a new set of functions and variables for some additional data, everything builds fine however when I run the code and it creates a new instance of that class as a boost::shared_ptr, then I immediately get a segfault in in QBasicAtomicInt::deref (this=0x0) at /usr/local/Trolltech/Qt-4.8.1/include/QtCore/qatomic_i386.h:132.
I can take it further as well, if I add just the getter and the variable, it'll no longer segfault. If I change the variable to a bool instead of a QString, it'll work, however if I change to a std::string it'll still segfault in the same place which is even weirder as the variable is no longer a Qt one. If I create a new instance of the class without it being a boost::shared_ptr it's fine, no problems at all. The functions aren't used internally, or externally in the class yet.
I had a similar problem.
I simply create a variable in a class and when try to use then cause segmentation fault and I was going crazy!
My environment is similar: A software using Qt on Linux write in C++ language.
So I clean the project and rebuild and then did work fine!
StackOverflow saves!
Trashed the directory and repulled the code, rebuild and it works. I don't know why, I'm guessing the clean process wasn't properly cleaning something out.

Adding invariant check to every method of a Class

I have a class with many methods and would like to check for pre/post conditions, such as is mMember == null and invoke function x() if not.
Is it possible to add pre/post conditions to every member of that class automatically? The class is defined in class.h and all methods are defined in class.cpp. Being able to define a macro at the start of the class.cpp would be ideal that would be inserted on every function entry in that file only.
I know I can manually add the same precondition/postcondition (using destructors) manually on every function entry but I keep running into this problem time and time again. This is not ideal as I can not check these changes in and must maintain them locally and they bit root from other people's changes.
I have not ever been able to do this in C++; I've always used a set of macros manually added to each member function.
It sounds like a job that might be well-suited to Aspect Oriented Programming, though, and I see that there are libraries out there for AOP in C++, such as AspectC++. It might be worth at least taking a look at one of these and seeing if it can be used to solve your problem.

Removing a compiled method from the .h file and its consequences

I have a binary that has always existed. It has a class C that it has always existed as well. We have to introduce a new method M to the class C but we only want some users to be aware of the existence of such method M.
By removing from the .h file such method, which problem we can introduce? Will such approach be backward compatible?
EDIT: We actually don't care if there's a way to find the method. We just want to make sure that only people knowing what they're doing, use it.
With most C++ compilers: if the method is virtual you'll be in serious trouble (the vtable will be all messed up); if the method is NOT virtual you shouldn't be (but some smart user will deduce the existence of what you're trying to hide via "security through obscurity" and find ways to use the method you'd rather keep hidden from him -- but that's another story;-).
A safer method would be to simply make a derived class and give that one's .h only to certain people.