473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

address of a function

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
5 1523
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
7
by: Nolan Martin | last post by:
is a static functions address constant? ie.. static void func(); write_to_file(&func); Restart program... static void func(); void (*funcPtr) ();
12
by: johny smith | last post by:
I am trying to figure out a way to print the address of what called a certain function once inside the function. I am assuming that this information is on the stack somewhere. But can someone...
35
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
6
by: Todd A. Anderson | last post by:
I have a function foo of which I need to get the address. The problem is that when you say "&foo" (or just foo for that matter), you get the address of this function's entry in a jump table and...
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
7
by: Ancient_Hacker | last post by:
In days of old, for no discernible reason, many CPU's had the ability to execute one or more instructions out of registers. Not very common today. Which brings up a semi interesting point. ...
36
by: Julienne Walker | last post by:
Ignoring implementation details and strictly following the C99 standard in terms of semantics, is there anything fundamentally flawed with describing the use of a (non-inline) function as an...
0
by: George3 | last post by:
Hello everyone, Some study these days about address of exported function in DLL. It is appreciated if you could review and comment. (for C++ DLL/EXE only) 1.Inside DLL
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.