By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,925 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,925 IT Pros & Developers. It's quick & easy.

address of a function

P: n/a
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

Jul 2 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
dost wrote:
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.

Jul 2 '06 #2

P: n/a
"dost" <ka**************@gmail.comwrote:
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/
Jul 2 '06 #3

P: n/a
dost schrieb:
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
Jul 2 '06 #4

P: n/a
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
Rolf Magnus wrote:
dost wrote:
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.
Jul 2 '06 #5

P: n/a
dost schrieb:
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
Jul 3 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.