473,569 Members | 2,676 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 1569
pr********@yaho o.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********@yaho o.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
"MemFunctui on() "

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********@yaho o.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
5628
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
2058
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 data-types of my choosing (eg an array of char, short, or int). The application has to do with generating checksum-type values for files or strings,...
2
2286
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 in Rectangle, the compiler tells me Point::x is protected. I would have expected Rectangle to see the protected members of any Point. Compiling the...
4
3386
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() this instanciates the class Y. Class X { run();
3
1331
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 necessary class declarations.
31
3141
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
2
2628
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 class BaseClass {
6
4572
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 would solve the problem, but it appears not to. I need this functionality to make object serialization a reality. Having to explicitly cast each...
6
8139
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 situation where I am implementing base class functions by including a pointer to subclass member in base class. Reason being functionality is...
0
7697
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7968
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6283
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
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 we have to send another system
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.