For example:
echo "change my case" | tr '[a-z]' '[A-Z]'
In my humble opinion ....
echo "change my case" | tr '[a-z]' '[A-Z]'
sed 's/^//g'
echo "bar" | sed 's/^/foo-/g'
Problem at hands
============
In the simplest of scenario, we basically want to prevent something like this:
class A
{
public:
A(){}
~A(){}
};
void foo(A a)
{
}
int main(int argc,char *argv[])
{
A a1;
A a2(a1); // directly invoking the copy constructor, should be disallowed
a2 = a1; // creating copies via assignment operator, should be disallowed
foo(a1); // and passing copies as parameters to other functions, should be disallowed
return 0;
}
class A
{
private: // copy constructor and assignment operator
A(const A&){}
A& operator=(const A&) {}
public:
A(){}
~A(){}
};
void foo(A a)
{
}
int main(int argc,char *argv[])
{
A a1;
A a2(a1); // error! Copy constructor is private
A a3;
a3 = a1; // error! assignment operator is private
foo(a1); // error! copies cannot be created since the copy constructor is private
return 0;
}
#include <iostream>
using namespace std;
class A
{
private: // copy constructor and assignment operator
A(const A&)
{
cout<<"Copy constructor"<<endl;
}
A& operator=(const A&)
{
cout<<"Assignment operator"<<endl;
return *this;
}
public:
A(){}
~A(){}
void violate1()
{
A tmp(*this); // copy constructor will get invoked
}
void violate2()
{
A tmp;
tmp = *this; // assignment operator will get invoked
}
};
int main(int argc,char *argv[])
{
A a1;
a1.violate1(); // gets invoked successfully
a1.violate2(); // gets invoked successfully
return 0;
}
#include <iostream>
using namespace std;
class A
{
private:
A(const A&); // copy constructor declaration only
A& operator=(const A&); // assignment operator declaration only
public:
A(){}
~A(){}
void violate1()
{
A tmp(*this); // copy constructor will get invoked
}
void violate2()
{
A tmp;
tmp = *this; // assignment operator will get invoked
}
};
int main(int argc,char *argv[])
{
A a1;
a1.violate1(); // would get compiled, however a linker error would be generated
a1.violate2(); // would get compiled, however a linker error would be generated
return 0;
}
We have achieved our goal! Copies of class A's object cannot be created now. But there is still room for improvement; It would definitely be more helpful if we can generate compile-time errors instead of link time errors. Sol3 will do just that/tmp/ccUfNEtK.o: In function `A::violate1()':
cpp_code.cpp:(.text._ZN1A8violate1Ev[A::violate1()]+0x14): undefined reference to `A::A(A const&)'
/tmp/ccUfNEtK.o: In function `A::violate2()':
cpp_code.cpp:(.text._ZN1A8violate2Ev[A::violate2()]+0x20): undefined reference to `A::operator=(A const&)'
collect2: ld returned 1 exit status
#include <iostream>
using namespace std;
class Uncopyable
{
private:
Uncopyable(const Uncopyable&); // copy constructor declaration only
Uncopyable& operator=(const Uncopyable&); // assignment operator declaration only
public:
Uncopyable(){};
~Uncopyable(){};
};
class A:public Uncopyable
{
public:
A(){}
~A(){}
void violate1()
{
A tmp(*this); // error! This cannot be invoked, since base class's copy constructor is private
}
void violate2()
{
A tmp;
tmp = *this; // error! assignment operator cannot be invoked, since base class has defined it as private
}
};
int main(int argc,char *argv[])
{
A a1;
A a2(a1); // error. Copy constructor cannot be invoked
a1.violate1(); // error (See above)
a2.viloate2(); // error (See above)
return 0;
}
If you are a developer and you post your code on blogger (or other blogs) then you are gonna love this small tool which I call ac2html (any code to html).
ac2html takes your code and highlights it using html thus allowing you to generate a syntax highlighted version of your code, which you can post on blogger or any place where html is accepted.
Sample run
========
Download ac2html from here. The code is platform independent and should run on all platforms (but I have only tested it on linux x86)
Untar it
tar -xvzf AnyCode2html.tar.gz
Compile it
cd AnyCode2htmlmake
Note that you might need to edit the Makefile to suit your system.
Now write some c++ code or pick an existing one. I am going to use a sample c++ file which has the following contents (copied and pasted from my source file)
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
// print a test message
cout<<endl<<"Hello world"<<endl;
return 0;
}
Ugly isn't it? Now run ac2html
./ac2html cpp_keywords cpp_code.cpp output.htmlBefore we show the results lets analyze the arguments that we just passed. All the arguments that we passed are required by ac2html.
Now copy the the contents of output.html and paste it to the blogger's compose window. Publish your post and you will get this:
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
// print a test message
cout<<endl<<"Hello world"<<endl;
return 0;
}
ACKNOWLEDGEMENTS
================
I got this idea from Adrian's post at http://codecentrix.blogspot.com/2007/05/blogs-and-c-syntax-highlighting.html. However his tool was too windows and c++ centric, hence I decided to rewrite a generic one.
mutable type specifiers, I have finally come to embrace this controversial concept (but its still in beta state :-)FixSession class for my FIXTransit project. I was passing FixSession objects as constant references to other classes, but realized that this wouldn't be possible because some logically-read-only functions might end up modifying the SessionState data member. For example Session::createHeader() whose job is to simply return a FIX message Header for that Session, would result in incrementing the SessionState::seqNum property.SessionState data member as mutable and continued to pass the Session objects as logically constant references :-)