//The following Program illustrates the working of const_cast
#include <iostream>
using namespace std;
void print (char * str)
{
cout<<"\nFrom Function print\n";
cout << str;
}
int main () {
const char * c = "Hello Friends";
cout<<"\nFrom main Function\n";
cout<<c;
print ( const_cast<char *>(c) );
return 0;
}
//The following given below is equivalent to the above Program
#include <iostream>
using namespace std;
void print (char * str)
{
cout<<"\nFrom Function print\n";
cout << str;
}
int main () {
const char * c = "Hello Friends";
cout<<"\nFrom main Function\n";
cout<<c;
print ( (char *)(c) );
return 0;
}
// The Program given below will not work.
#include <iostream>
using namespace std;
void print (char * str)
{
cout<<"\nFrom Function print\n";
cout << str;
}
int main () {
const char * c = "Hello Friends";
cout<<"\nFrom main Function\n";
cout<<c;
print ( c );
return 0;
}
#include <iostream>
using namespace std;
void print (char * str)
{
cout<<"\nFrom Function print\n";
cout << str;
}
int main () {
const char * c = "Hello Friends";
cout<<"\nFrom main Function\n";
cout<<c;
print ( const_cast<char *>(c) );
return 0;
}
//The following given below is equivalent to the above Program
#include <iostream>
using namespace std;
void print (char * str)
{
cout<<"\nFrom Function print\n";
cout << str;
}
int main () {
const char * c = "Hello Friends";
cout<<"\nFrom main Function\n";
cout<<c;
print ( (char *)(c) );
return 0;
}
// The Program given below will not work.
#include <iostream>
using namespace std;
void print (char * str)
{
cout<<"\nFrom Function print\n";
cout << str;
}
int main () {
const char * c = "Hello Friends";
cout<<"\nFrom main Function\n";
cout<<c;
print ( c );
return 0;
}
Click here to download the C++ Program.
No comments:
Post a Comment