Eclipse CDT "New Class" Template - templates

I have been using Eclipse CDT for some time now, and I love it, but there are a few tedious things that I would like to fix up about it.
When you create a new file, one of the options is "New"->"Class". I was wondering if anyone knows of a way to edit the "${declarations}" section of this "Class" template.
To be more specific, I have looked through the "Window"->"Preferences" menu and been unable to find anything. I have changed both "C/C++"->"Code Style"->"Code Templates" and "C/C++"->"Editor"->"Templates". Only the first of the two actually seems to change what appears upon class creation, and it doesn't let me change what is in the "${declarations}" section. Does anyone know how to change this?
Thanks,
Chris

The preference C/C++ -> Editor -> Templates is used by the templates which are inserted manually via Context Assist. Try create a new file, type clas and press ctrl+space for context assist. You should get two assist proposals: a keyword proposal and a template proposal (the latter will also be triggered automatically as default if you type class and press ctrl+space).
Upon selection of the template proposal, a class body will be generated according to the template which you can define in this preference.
As for C/C++ -> Code Style -> Code Templates, this is used in automatic generation. When you use the New Class wizard, the Default C++ Source template and Default C++ Header template are used and the $(declarations) variable is replaced by whatever code is generated by the New Class wizard.
This would mean that you can use a custom template by triggering one of the Editor templates manually, possibly via creating a named class with New Class wizard and then replacing the default class body in header by your custom template.
Or do you suggest that the New Class wizard lacks any important fields and should be extended?

Or do you suggest that the New Class wizard lacks any important fields and should be extended?
Yes private copy and assign operator.

Oooh! Finally found an answer.
In Eclipse CDT (Juno), go to File -> Preferences.
Navigate to the following pane on the left panel of the Preferences window: C/C++ -> Code Style -> Code Templates.
In the Code Templates window, navigate to Files -> C++ Header File.
Edit the header file:
Remove the ${declarations} line, and insert your own stuff.
This has the somewhat unfortunate disadvantage of losing everything that the New Class Wizard would produce. This might invalidate any inherited classes or other things you might select from the New Class Wizard, but I haven't checked.
Here's my C++ Header File Template:
${filecomment}
#ifndef ${include_guard_symbol}
#define ${include_guard_symbol}
${includes}
${namespace_begin}
class ${type_name}
{
public:
/* Default Constructor */
${type_name}();
/* Deconstructor */
virtual ~${type_name}();
private:
/* Disable the following by default */
${type_name}(const ${type_name}& other);
${type_name}& operator=(const ${type_name}&);
};
${namespace_end}
#endif /* ifndef ${include_guard_symbol} */
Hope this helps!

Related

How to add form to existing class?

I have an existing (QCreator build) class derived from QTextEdit
class CCC_MdiChild : public QTextEdit
I like to add GUI form like so
I have no issue creating new form.ui and adding it to the project.
I do not know how to modify existing constructor ( and its definition ) to include "form.ui" .
I am asking for an assistance / help to do that.
Here is my current constructor
Sorry , I did try to format the code but I could not "cut and paste " it as code .
( and I could not reedit it after it was edited once )
Since I did realize my poor formatting any automated comments in that aspect will be ....
The required steps are documented here. For completeness, they are:
Since you've added the .ui file to the project, the UI compiler should generate the corresponding header ui_form.h, which you should #include in the header for your class;
Add a private member to your class of the type defined in this header, which is in namespace Ui and takes its name from your .ui file;
Call the object's setup function as setupUi(this) in the constructor(s) of your class.
Alternatively, you also use multiple inheritance, or avoid including ui headers in header files with the slightly more complicated forward-declared pointer member approach.

vscode shortcut or easier method to add a functions to class

I went up and down of the vscode documentation to find a way to automatically create functions inside the class, every time I am adding a method to a class I need to copy the definition and scroll to the bottom of the page to implement the method.
Clion had a great set of tools for this
https://www.jetbrains.com/help/resharper/Code_Generation_in_CPP.html#equality
I wonder if there is any easy way that I can use to add a function to a class without scroll up and down the file
class HelloWorld
{
private:
public:
int IWantToDefineAMethod(); //<- need to scroll here and write this
};
int HelloWorld::IWantToDefineAMethod() //<-- then need to scroll to here and write this
{
}
// and need to scroll here and there couple of time just to add a single function!
https://marketplace.visualstudio.com/items?itemName=amiralizadeh9480.cpp-helper
I'm not the creator and just found it.
A side note is that it creates implementation for prototype but it doesn't check if it's duplicated which is described in the Known ISSUE section.
You need to set CppHelper.SourcePattern to let plugin knows where should it search for source file.
Example:
"CppHelper.SourcePattern": [
"{FILE}.cpp",
"{FILE}.c",
"{FILE}.inl",
"/src/{FILE}.cpp"
]
Where {FILE} is your active header file name.

Shortcut to create C++ class method?

I have class definition code with function to be crated name
class CAAA
{
public:
public_method
}
I expect to create function stub in header and .cpp with help of some shortcut.
Trying to create class method in fast way:
Shortcut ctrl+1 brings No suggestions available:
Shortcut ctrl+space brings No Default proposal:
How to solve this problem?

Qt: Generate ui_ classes using uic, load dynamically by class name

Is it possible to load an .ui class generated by uic, dynamically by class name? I need to decide which UI class to load dynamically. I do not have that information at compile time. I do not want to use QUiLoader. Instead, I want to combine the direct approach here with QMetaType object instantiation by string.
I.e.
add the UI file to FORMS in project file,
declare the UI classes for QMetaType usage Q_DECLARE_METATYPE(Ui::planar_break) or Q_DECLARE_METATYPE(Ui_planar_break)
then form a string class name dynamically depending on user action: "Ui::planar_break" or "Ui_planar_break"
and invoke the below function to get the UI widget pointer for usage?
QWidget* initiateClassByName(QString name){
int id = QMetaType::type(name.toLatin1());
QWidget* widget=nullptr;
if (id != QMetaType::UnknownType) {
widget=static_cast< QWidget* > (QMetaType::create(id));
//QMetaType::destroy(id, myClassPtr);
//myClassPtr = 0;
}
return widget;
}
I am trying to increase the performance, compared to loading a dozen or so UI files (stored in Qt resource files) dynamically every time a specific dialog is instantiated. When I do this I seem to get a QMetaType::UnknownType every time. Ideas? Thanks.
(Hm, not sure why my function wasn't showing as code block here, until I made it a quotation.)
uic creates C++ code. If you really want to dynamically create a widget/dialog out of an xml file at runtime, you need to use the Qt Ui Tools. The class QUiLoader might be what you are looking for. If you do this, you can query the created QWidget through QWidget::findChild You can interact with the UI items through QObject::findChild(), provided you give your widgets distinct and meaningful object names.
Essentially, based on discussion had in #qt irc channel on Freenode I think what I am asking is actually not feasible.
My understanding is that even if I could get the C++ headers compiled and the classes registered using perhaps also qRegisterMetaType(), and perhaps even get them instantiated using QMetaType, to actually get the UI built I would have to call setupUi() on the instance.
Since UI files do not implement a common interface that includes setupUi, and I don't know compile-time which class I am instantiating, calling setupUi becomes impossible.

In iOS how do I change the text of a button, from a class other than viewController?

I would like to change the text of my UIBarButtonItem from another class (objective-C++) that I use in my project.
I have an IBOutlet myButton setup in myViewController and I can successfully do something like:
[ myButton setTitle:#"newTitle" ];
in myViewController.mm
Now I would like to do the same but from myCppClass that I use in my project.
Is there a way for myCppClass to access myViewController's myButton?
Shall I use some type of delegation mechanism?
I am pretty new to Ios and objective-C.
Thanks,
Baba
Create a method within your myViewController class to change the button title, then call that method from myCppClass by following the instructions described in this answer:
How to call method from one class in another (iOS)
https://stackoverflow.com/a/9731162/2274694
The short answer is, don't. You should treat a view controller's views as private. Instead, add a method to your VC like changeButtonTitle. Then call that method from from your other class.
The answers above are correct, but from your comments I suspect you aren't yet happy.
If you are super lazy, and you don't mind the string being the same in all instances of the VC (which in practice is usually the case) you could simply write a getter and setter for the string name as a class variable in the destination class. That way you don't even need access to the actual instance of the class, just its name. Tacky but super easy.
As others have pointed out, don't try and modify the buttons on a different VC directly. Pass a message and have the owning VC do it when it loads.
Well, passing messages forwards (to a new VC) is very easy. At the bottom of every VC class code there is #pragma navigation section commented out which gives you a handle to the destination VC. Cast it to the proper type and you set properties in the destination VC instance. In your case, create a public property NSString which holds the button text in your destination VC, and set it in your navigation section. This could be any class, or even a delegate, but a simple string should work.
Passing messages backwards (to previous VCs) can work the same way but it starts to get messy. You can programatically step back through the stack of VCs to find a particular (instance of) a VC. One of the answers to Get to UIViewController from UIView? gives sample code for stepping back through view controllers.
But if its simply forward communication, passing messages or information through
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
at the bottom of the VC code and the commented out lines below is very easy and safe.