[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: Re: [linux-india-programmers] C++ question



----- Original Message -----
From: Deepak Chandrashekar <deepakc@xxxxxxxxxx>

Hi Gautam,

 You can violate C++ concepts and even go ahead and access private variables
and
private virtual functions and as deepak rightly stated this is probably one
of the reasons
why java has no pointers. I send a sample code which will allow u to acess
private variables
and private virtual functions

#include <iostream.h>
class tp
{
    int x;
    float y;
    char z;
public:
    tp():x(0),y(10.0),z('A') {  }
};  /* End of class */

int main()
{
    void* p;
    tp t;
    p =(int*) (&t);
    cout<<*((int*)p)<<endl;
    ((int*)p)++;
    cout<<*((float*)p)<<endl;

    ((float*)p)++;
    cout<<*((char*)p)<<endl;
    return 0;
}

The above code allows u to access private variables of a class by explicit
typecasting of the "&object."
If u are interested in the code to access private virtual functions i can
send it later
coz i don't wan't to crowd this mail unnecessary
regards

- Mahesh
mahesh@xxxxxxx



To: <linux-india-programmers@xxxxxxxxxxx>;
<linux-india-programmers@xxxxxxxxxxxxxxxxxxxxx>
Sent: Friday, July 07, 2000 5:38 AM
Subject: [LIP] Re: [linux-india-programmers] C++ question


> On Thu, 6 Jul 2000, G Gautam wrote:
>
> > Hi,
> >
> > This isn't a linux question but it does happen with g++. So that's why I
have posted here.
> >
> > #include<iostream.h>
> >
> > class tmp
> > {
> >    private:
> >    int var;
> >    public:
> >    void set( int i )
> >    {
> >    var = i;
> >    };
> > };
> >
> > main()
> > {
> > int *ptr;
> > tmp c;
> >
> > c.set( 200 );
> > ptr = ( int* ) &c;
> >
> > cout << *ptr;
> > return 0;
> > }
> >
> > I really should not be seeing the value of var, right? I would
appreciate any feedback.
> >
> > Thanks
> > Gautam
> >
> > ------------------------------------------------------------------------
> > Who invented Gatorade -- and what part did it play in
> > winning the1967 Orange Bowl? Find out the true facts at
> > http://click.egroups.com/1/6212/8/_/30164/_/962898033/
> > ------------------------------------------------------------------------
> >
>
>
> -----------------------------------------------------------------------
> For more information on the LIP mailing list see:
> http://lists.linux-india.org/lists/LIP
>