Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,671 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,186 people online right now. Registration is fast and FREE... Join Now!




Static Member Functions

 
Reply to this topicStart new topic

Static Member Functions, Its well known that an object encapsulates non-static method and data

abhi_dill
post 2 Sep, 2007 - 07:21 AM
Post #1


New D.I.C Head

*
Joined: 6 Aug, 2006
Posts: 18


My Contributions


Hello,
please see these codes

CODE

#include<iostream>

using namespace std;

class A
{
public:
    static void sayHello()
    {
        cout<<"Hello World!!!";
    }
    void SayHwztht()
    {
        cout<<"hwz that possible!!!";
    }

};

int main()
{
  A* ab;
  ab = NULL; //i have just declaterd a pointer there is no actual
//object created as i havent used a 'new' for doing same
  ab->sayHello();
  ab->SayHwztht();
  system("pause");
  return 0;
}


its being said in C++ tht the object encapsulates the member funtions and the member variables, that is whenever a object is created new copy of those member var and func is also generated but if we have a static member function only the defination exist in class but not in all objects

so as by the rule
CODE
ab->sayHello();
should have worked as it is a static member but
CODE
ab->SayHwztht();
should not work as it is not static and i haven't made ne new object yet only pointer/refrence

o->when i made the same prog in JAVA, JAVA gave me error icon_up.gif saying that sayHwztht() can not be accessed and as expeccted sayHello() worked as its static
o->I used DEVCpp 4.9.9.2, VS2005, VS98 all gave same result

and the result is
CODE
Hello World!!!hwz that possible!!!Press any key to continue . . .


Y is that i can access both functions??? blink.gif
User is offlineProfile CardPM

Go to the top of the page

musya
post 2 Sep, 2007 - 08:07 AM
Post #2


D.I.C Regular

Group Icon
Joined: 25 Apr, 2007
Posts: 291



Thanked 1 times

Dream Kudos: 50
My Contributions


QUOTE(abhi_dill @ 2 Sep, 2007 - 08:21 AM) *

Hello,
please see these codes

CODE

#include<iostream>

using namespace std;

class A
{
public:
    static void sayHello()
    {
        cout<<"Hello World!!!";
    }
    void SayHwztht()
    {
        cout<<"hwz that possible!!!";
    }

};

int main()
{
  A* ab;
  ab = NULL; //i have just declaterd a pointer there is no actual
//object created as i havent used a 'new' for doing same
  ab->sayHello();
  ab->SayHwztht();
  system("pause");
  return 0;
}


its being said in C++ tht the object encapsulates the member funtions and the member variables, that is whenever a object is created new copy of those member var and func is also generated but if we have a static member function only the defination exist in class but not in all objects

so as by the rule
CODE
ab->sayHello();
should have worked as it is a static member but
CODE
ab->SayHwztht();
should not work as it is not static and i haven't made ne new object yet only pointer/refrence

o->when i made the same prog in JAVA, JAVA gave me error icon_up.gif saying that sayHwztht() can not be accessed and as expeccted sayHello() worked as its static
o->I used DEVCpp 4.9.9.2, VS2005, VS98 all gave same result

and the result is
CODE
Hello World!!!hwz that possible!!!Press any key to continue . . .


Y is that i can access both functions??? blink.gif


In c++ a static member means that is the same for all objects inheriting or composing from the class, so you can access them no problem thats just the way c++ is. Correct me if im wrong guys, i have a hard time explaining this for some reason.
User is offlineProfile CardPM

Go to the top of the page

abhi_dill
post 2 Sep, 2007 - 08:10 AM
Post #3


New D.I.C Head

*
Joined: 6 Aug, 2006
Posts: 18


My Contributions


yeah tht what m saying juss go through again I called static its done (good going c++) but i called non-static member function (sayHwztht() ) it worked thts my qn smile.gif
User is offlineProfile CardPM

Go to the top of the page

Bench
post 2 Sep, 2007 - 08:43 AM
Post #4


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 602



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


C++ isn't java - the syntax is all perfectly legal, so there's no reason the compiler should throw an error.

The program itself seems to be running on undefined behaviour - since you're dereferencing a null pointer. As to why you got that output on the compilers you tried, what you've probably got, from an assembly point of view, is code which simply invokes a function pointer. You just got lucky (or unlucky) that on the platforms you tried, your function pointer actually pointed to a valid function.

With a null pointer to a class, there is no requirement AFAIK that its associated methods be valid function pointers (Indeed, if the function were virtual, then you'd be missing a Vtable entry, and the program would almost certainly crash)
User is offlineProfile CardPM

Go to the top of the page

abhi_dill
post 2 Sep, 2007 - 08:57 AM
Post #5


New D.I.C Head

*
Joined: 6 Aug, 2006
Posts: 18


My Contributions


hey Sry wid tht but when i was debugging tht time i made it NULL and u do it widout that statement output is same

yeah its true that this works wid static member function but not wid static member variable as expected (shuu!!! atleast here its working) and as u said the prog crashed when i tried tht but here.. its different smile.gif
User is offlineProfile CardPM

Go to the top of the page

abhi_dill
post 2 Sep, 2007 - 09:03 AM
Post #6


New D.I.C Head

*
Joined: 6 Aug, 2006
Posts: 18


My Contributions


and if we talk wid JAVA comparision then i mean the concept of encapsulation.... yeah the syntax were different but there too i made refrence not instance... so weight is on concept of OO not language rush smile.gif
thanks anyways
User is offlineProfile CardPM

Go to the top of the page

Xing
post 2 Sep, 2007 - 05:38 PM
Post #7


D.I.C Addict

Group Icon
Joined: 22 Jul, 2006
Posts: 723



Thanked 2 times

Dream Kudos: 1575
My Contributions


For documentation purposes, calls to static member functions should be coded as Classname::staticMember() rather than as object.staticMember() or ptr->staticMember(). The :: is a reminder that the member function is statically bound and that the member function is attached to the class rather than to an individual object of the class.
User is offlineProfile CardPM

Go to the top of the page

abhi_dill
post 2 Sep, 2007 - 06:37 PM
Post #8


New D.I.C Head

*
Joined: 6 Aug, 2006
Posts: 18


My Contributions


@Xing
thanks sir, but actually my qn is not for accessing static member function (it can be accessed as by the rule) da qn is y i can access 'non-static' member function as its not in class it is in each object
User is offlineProfile CardPM

Go to the top of the page

Xing
post 2 Sep, 2007 - 08:34 PM
Post #9


D.I.C Addict

Group Icon
Joined: 22 Jul, 2006
Posts: 723



Thanked 2 times

Dream Kudos: 1575
My Contributions


QUOTE(abhi_dill @ 3 Sep, 2007 - 08:07 AM) *

@Xing
thanks sir, but actually my qn is not for accessing static member function (it can be accessed as by the rule) da qn is y i can access 'non-static' member function as its not in class it is in each object

I would suggest you to read these
http://www.cprogramming.com/tutorial/lesson12.html
http://www.functionx.com/cpp/Lesson20.htm
User is offlineProfile CardPM

Go to the top of the page

Bench
post 3 Sep, 2007 - 03:31 PM
Post #10


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 602



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


QUOTE(abhi_dill @ 2 Sep, 2007 - 05:57 PM) *

hey Sry wid tht but when i was debugging tht time i made it NULL and u do it widout that statement output is same

yeah its true that this works wid static member function but not wid static member variable as expected (shuu!!! atleast here its working) and as u said the prog crashed when i tried tht but here.. its different smile.gif

Making it non-null doesn't change anything.. In fact, it may even make things worse, because you're potentially accessing memory that doesn't belong to you. in either case, there's no valid object at the end of the pointer, so its still undefined behaviour, and the reason you got lucky that it worked on your compiler is still probably down to behind-the-scenes function pointers.

(If you wanted to check, you could try disassembling the resulting .exe and see whats going on under the hood)
User is offlineProfile CardPM

Go to the top of the page

abhi_dill
post 4 Sep, 2007 - 01:31 AM
Post #11


New D.I.C Head

*
Joined: 6 Aug, 2006
Posts: 18


My Contributions


@Bench
1stly its not pointing anywhere (try to debug this u will find it out)
2ndly this is a pointer which can store address of object type 'A' so actully no such object exist
3rd unfortunately this worked fine wid every compiler smile.gif
still thanks for ur help
User is offlineProfile CardPM

Go to the top of the page

Bench
post 4 Sep, 2007 - 02:08 AM
Post #12


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 602



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


QUOTE(abhi_dill @ 4 Sep, 2007 - 10:31 AM) *

@Bench
1stly its not pointing anywhere (try to debug this u will find it out)
2ndly this is a pointer which can store address of object type 'A' so actully no such object exist
3rd unfortunately this worked fine wid every compiler smile.gif
still thanks for ur help

1. If its not null then it must be pointing to somewhere. Even if your debugger doesn't show it - any uninitialised variable will contain data from whatever junk was left over before, which, for a pointer, boils down to some memory location that doesn't belong to your program. (Its also possible that the 'junk' is a null value too)

2. The class 'A' defines a member function - it is allowed for this function to exist even when no object of type 'A' does. The compiler may call that function with an invalid this pointer. Since your member function doesn't perform any operation on the (non-existant) object, you were lucky to get away with no ill side effects.

3. It might have worked with every compiler you tested it on - that doesn't mean you can rely on its behaviour. The standard says that its undefined, therefore you mustn't rely on it
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:03AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month