address of a function

D

dost

as in a class private section we can access the private data member in
main through the pointers...is their any way to access the member
functions which are written in private section.....


class Myclass{
private:
int a;
void disp()
{
cout<<"hello"
}
};
void main
{
Myclass obj;
int *p=(int *)&obj;
*p=50;//the value of a is chjanged now
}

thank you
kapil kaushik
 
R

Rolf Magnus

dost said:
as in a class private section we can access the private data member in
main through the pointers...

Yes, but it defeats the purpose of making them private in the first place.
is their any way to access the member functions which are written in
private section.....

Make the caller a friend.
class Myclass{
private:
int a;
void disp()
{
cout<<"hello"

Error: cout undefined.
Error: semicolon missing.
}
};
void main

Error: main must return int.
Error: no parenthesis after function name in declaration.
{
Myclass obj;
int *p=(int *)&obj;

The result of this operation is implementation defined. Don't do it, it's a
very ugly hack. And better use the C++ casts instead of the old C style
cast.
*p=50;//the value of a is chjanged now
}

It might be.
 
R

Robbie Hatley

dost said:
In a class private section we can access the private data
member in main through the pointers.

Only if you did some evil casting stuff, using knowlege
of the size of the class, the size of the data members,
and what padding or packing is being used.

But if you're going to violate encapsulation, then why use
"private" to begin with? Just use a struct, with no access
specifiers, so all the members are public.
Is their any way to access the member functions which
are written in private section?

Not directly. You'd have to do that through a function
declared in the "public" section of your class.

So why not make your functions "public" and data "private"?
Let your member functions do the work on the data and report
their results back to the outside world. That's the usual
way to do OOP.

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
 
T

Thomas J. Gritzan

dost said:
as in a class private section we can access the private data member in
main through the pointers...is their any way to access the member
functions which are written in private section.....


class Myclass{
private:
int a;
void disp()
{
cout<<"hello"
}
};
void main
{
Myclass obj;
int *p=(int *)&obj;
*p=50;//the value of a is chjanged now
}

It is undefined behaviour. It just maybe works in this case. Try to make
disp() virtual and check if it still works.

(but this example not even compiles)

Thomas
 
D

dost

sorry for some errors which i forgot in writing ..but please ignore
that and tell me the way to get out of the problem ...function should
be access through main directly....is there any way?

thankyou
 
T

Thomas J. Gritzan

dost said:
sorry for some errors which i forgot in writing ..but please ignore
that and tell me the way to get out of the problem ...function should
be access through main directly....is there any way?

Replace "private:" by "public:" and don't top-post.

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top