473,503 Members | 2,197 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing a Meber function through a pointer to a class

#include <iostream.h>
class Simple {
public :
void memfunction()
{
cout << "Hello I am in memFunction" ;
}
};

Simple *objSimple ;

void wrapper()
{

/* how we are able to access the member function , when we had not
created an object ? then wht's the use of Static meber functions ?
*/
objSimple->memfunction();
}

int main()
{
wrapper();
exit(0);
}

how wrapper function is calling the member function , without creating
the object ?
Thanks in advance .

Jul 23 '05 #1
4 1564
pr********@yahoo.com wrote:
#include <iostream.h>
class Simple {
public :
void memfunction()
{
cout << "Hello I am in memFunction" ;
}
};

Simple *objSimple ;

void wrapper()
{

/* how we are able to access the member function , when we had not
created an object ? then wht's the use of Static meber functions ?
*/
objSimple->memfunction();
}

int main()
{
wrapper();
exit(0);
}

how wrapper function is calling the member function , without creating
the object ?
Thanks in advance .


objSimple has not been initialized explicitly. So it is initialized to
zero as it has static storage. Dereferencing a null pointer is undefined
behaviour. So after executing objSimple->memfunction your program is
in error and anything can happen...

Tom
Jul 23 '05 #2
pr********@yahoo.com wrote:
[snip] void wrapper()
{

/* how we are able to access the member function , when we had not
created an object ? then wht's the use of Static meber functions ?
*/
objSimple->memfunction();
}

int main()
{
wrapper();
exit(0);
}

how wrapper function is calling the member function , without creating
the object ?


The answer is:
It seems to work in that specific case but is nevertheless a faulty program.
Anything could have happend and that includes: seems to work.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #3
A friend of mine had following explaination..

/**
When ever we call an object's function, the compiler converts the call
to something like this...

Check the example..

You have a class base
{

Public:
hello(int x);
};

Object obj is the object of type Base.
Now if you call obj.hello (4) ;
The compiler converts this call to
F1_hello(4, obj).. Check the second argument.. That is object which is
calling the function.
Now if obj was an un initialized pointer, it can be null or having
garbage values.

In your example, they are not altering any data members in the function
"MemFunctuion() "

So the call now becomes
F1_memFunction(objSimple);

Since we don't refer any data members of the class Simple, it works..
If you refer some data members.. It will crash..
This also shows that objects of a class doesn't have individual copies
of member functions.. They share a common set of member functions.
**/

Jul 23 '05 #4

pr********@yahoo.com wrote:
[code]
how wrapper function is calling the member function , without creating the object ?
Thanks in advance .


This is undefined behaviour and may work in some cases and maynot in
other. However, I am pretty sure if you try to access a data memebr
instead of the member function in wrapper, you should get a
segmentation fault.

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5623
by: Abhijit Deshpande | last post by:
Is there any elegant way to acheive following: class Base { public: Base() {} virtual ~Base() {} virtual void Method() { cout << "Base::Method called"; return; } };
1
2055
by: J. Campbell | last post by:
I have a feeling that I'm doing things all ass-backwards (again ;-), and would like some advice. What I want to do is: put some data to memory and then access that memory space as an array of...
2
2268
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point...
4
3381
by: keepyourstupidspam | last post by:
Hello, I have a class X with a member function setConfigValues(). I want to access this member function in another class Y. Y is not inherited from X. A member function of class X called run()...
3
1323
by: Dave | last post by:
I've seen at least one article on this: How to access classes, etc. managed-to-unmanaged. I can't find it. Any tricks or pointers? I guess the class name won't get munged but I'm not clear about...
31
3112
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
2
2622
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh...
6
4566
by: Me | last post by:
I need to be able to acces non-virtual members of sublcasses via a base class pointer...and without the need for an explicit type cast. I thought a pure virtual getPtr() that acts as a type cast...
6
8132
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a...
0
7093
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
7287
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,...
1
7008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5594
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.