Sunday, June 18, 2017

-------------------------------------------------------------------------------
How to get rid of compiler error-
deprecated conversion from string constant to ‘char*’
I got this compilation error while compiling the below code.

void ABC(char *strng) {};
ABC("Hello World");
or
void ABC(char *strng){};               //This code may compile but the window will crash on execution
char *strng1="Hellow World";
ABC(strng1);

I find two solution for this...
1.
void ABC(char *strng){};
char strng1[] ="Hellow World";
ABC(strng1);

2. define const every where if the sting does no changes.
void ABC(const char *strng){};
const char *strng1 ="Hellow World";
ABC(strng1);


---------------------------------------------------------------------------
Access the private data member variables of the class without friend function.
#include <iostream>
#include <string>
using namespace std;
class ABC
{ private:
   int a,b;
  public:
  void show() {
      cout<<"a="<<a<<"and b="<<b; }
};
int main()
{
    ABC obj;
    int *p_obj=(int*)&obj;
    *p_obj=10;                           // It will set the value of a to 10;
     *(p_obj+1) = 20;                 //It will set the value of b to 20.
    obj.show();
}

No comments:

Post a Comment