what are member function??
what is the use of :: in them ??
what are static and non static member function??

Member Avatar for MonsieurPointer

A member function is a method (or function) which is contained (declared) in a class.

The :: indicates that anything to the right of :: is the name of the member function. For example, Foo::Bar shows that the method/function Bar is a member of class Foo.

A static member function allows the call the member/function without having to instantiate (create an instance / create an object) of a class, whereas an instance of the class' member function is required to call the non-static member function. The keyword static tells the compiler that a member function is static.

Does that make sense?

instance of the class' member function is required to call the non-static member function

Maybe I misinterpreted that, non-static member functions can call both static and non-static member functions. Static member functions can only call other static member functions and use static class data.

Member Avatar for MonsieurPointer

@Ancient Dragon:
Yes, indeed. I just wasn't sure if that explanation was in scope of his question. If an example is required, I will be more than happy to provide one. :)

The simple explanation of static members:

Static members exist on their own, but are members of the class. They can be accessed by instance objects of the class, or other stuff that has permissions to access it.

Non-static members only exist within an instance object of the class. They can ONLY be accessed within the context of an instance object.

Everything else was said before... :-)

what is the use of :: in them ??

A simple trick, just think of Foo::Bar as meaning "Bar from Foo". This meaning works everywhere you see those ::. Like std::cout is "cout from std namespace". Or MyClass::SomeFunction() means "call SomeFunction() from MyClass". In fancy C++ terminology, the :: is called the "scope resolution operator", because in an expression like Foo::Bar, the operator :: tells you to what scope Bar belongs to (i.e., it "resolves" the scope of Bar).

what are member function??

Functions that belong to a class. And when something belongs to a class, it is referred to by something like MyClass::SomeFunction(), as in, "SomeFunction from MyClass", meaning SomeFunction belongs to MyClass, it is a member function.

what are static and non static member function??

Both types of functions belong to the class (i.e., member functions), but non-static member functions (not declared with the static keyword) must be called on an object of the class they belong to. Calling it as MyObj.SomeFunction() means "call SomeFunction on the object 'MyObj'". Calling it as MyObjPtr->SomeFunction() means "call SomeFunction on the object that 'MyObjPtr' points to". As for static member functions, they are just called by being resolved from the class scope, i.e., as MyClass::SomeFunction().

If you are already within the class scope (in the body of a member function, for example), then you don't need to resolve the scope of the static member functions, i.e., no need for the MyClass:: part. If you are already within a non-static member function's body, you don't need to resolve the object on which you call another non-static member function, if you are calling it on the same object (pointed to by the this pointer). These types of scoping rules apply almost everywhere in C++.

ok thankx..but still hv 1 query ..i.e. static member can use "this" pointer and non static can't use "this" pointer...
Q1. what is "this" pointer?

Member Avatar for MonsieurPointer

The "this" keyword refers to the instance of the class which "this" is being called in. For example, if you were to use "this" in the Foo class, then "this" is representing an instance of Foo.
Let's suppose that class Foo has another class member int fubar, and we want to change its value in Foo::Bar to 2:

void Foo::Bar(void)
{
     this->fubar = 2;
}

In many situations, the "this" keyword is not explicitly required, but helps distinguish between class members and non-class members. In theory, the above example would work also as follows:

void Foo::Bar(void)
{
     fubar = 2;
}

but only as long as fubar is a class member of Foo.
In the following example, the class member Bar is now calling class member Fox:

void Foo::Bar(void)
{
     this->Fox();
}

which is equivalent to

void Foo::Bar(void)
{
     Fox();
}

but again only if Fox() is a class member of Foo.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.