I am looking for an elegant solution for implementing the equivalent of the C# using statement in C++. Ideally the resultant syntax should be simple to use and read.
C# Using statement details are here - http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.80).aspx
I am not sure whether the solution would be to use function pointers with destructors on classes, some form of clever template programming or even meta template programming. Basically I do not know where to start with this...
Rust and C++ are the best alternatives for systems programming languages that have the same performance characteristics as C. Go's performance is similar to Java and C# due to its runtime and garbage collection.
The implementation ( . c ) file contains implementations of operations (methods) whose prototypes are defined in the specification file. For example, when you run the HelloWorld sample, one of the generated files is Default.
The closest thing you can get is a struct .
This document describes the simplest possible coding style for making classes in C. It will describe constructors, instance variables, instance methods, class variables, class methods, inheritance, polymorphism, namespaces with aliasing and put it all together in an example project.
You don't need to implement this in C++ because the standard pattern of RAII already does what you need.
{
ofstream myfile;
myfile.open("hello.txt");
myfile << "Hello\n";
}
When the block scope ends, myfile
is destroyed which closes the file and frees any resources associated with the object.
The reason the using
statement exists in C# is to provide some syntactic sugar around try/finally and IDisposable
. It is simply not needed in C++ because the two languages differ and the problem is solved differently in each language.
First, we have to define a Closeable/Disposable public interface:
#include <iostream>
using namespace std;
class Disposable{
private:
int disposed=0;
public:
int notDisposed(){
return !disposed;
}
void doDispose(){
disposed = true;
dispose();
}
virtual void dispose(){}
};
Then we should define a macro for the using keyword:
#define using(obj) for(Disposable *__tmpPtr=obj;__tmpPtr->notDisposed();__tmpPtr->doDispose())
and; here is an example application:
class Connection : public Disposable {
private:
Connection *previous=nullptr;
public:
static Connection *instance;
Connection(){
previous=instance;
instance=this;
}
void dispose(){
delete instance;
instance = previous;
}
};
Connection *Connection::instance = nullptr;
int Execute(const char* query){
if(Connection::instance == nullptr){
cout << "------- No Connection -------" << endl;
cout << query << endl;
cout << "------------------------------" << endl;
cout << endl;
return -1;//throw some Exception
}
cout << "------ Execution Result ------" << endl;
cout << query << endl;
cout << "------------------------------" << endl;
cout << endl;
return 0;
}
int main(int argc, const char * argv[]) {
using(new Connection())
{
Execute("SELECT King FROM goats");//in the scope
}
Execute("SELECT * FROM goats");//out of the scope
}
But if you want to delete variables automatically from memory, you can simply use braces {}
; therefore, every variable inside of the scope will be removed at the end of the scope.
here is an example:
int main(int argc, const char * argv[]) {
{
int i=23;
}
// the variable i has been deleted from the momery at here.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With