I was trying to access the private variable from a class using a public function.
class myCls : public CFormView
{
private:
CString pvtMem;
public:
CString getPvtmem() { return pvtMem; }
}
I did something like this:
myCls *lmyCls = new myCls() ;
CString *lPvtMem = lmyCls->getPvtmem();
but I got error c2248
then I changed pvtMem from private to public.
But i ended up in same error.
I tried to access pvtMem directly using object since its public.
But dint seem to work.
I did all sorts of hits like
myCls lmyCls;
CString lPvtMem = lmyCls.pvtMem ;
Compiler is not liking anything 🙁
well, myCls is defined in file BatchDlg.h which i #included in
my calling class.
I did add class reference for the last hit like class myCls in calling class.
In your first example, you return “CString” but assign it to “CString *” (a pointer).
In the second example you are calling the class’ operator=() method (assignment operator). Changing “lPvtMem” will modify it’s copy of the variable not the original variable.
Try:
Now the function returns a pointer to the private member and the assignment is to a pointer to the CString class. Now modifying lPvtMem will modify the object inside the class.