The this pointer when c++--inherits

Source: Internet
Author: User


The 1.this pointer is used only in member functions of the class, and the this pointer is used when the member function of the class needs to use its own pointer. However, a static function cannot use the This keyword, and its interpretation is that because this is a reference, which object calls the method and which object is referenced. The static method may not be called by the object, this is not referenced, that is: the static method belongs to the entire class, this refers to the current object. Like what:
Class Examthis
{
int showthis ()//defines a member function that displays its own pointer.
{
printf ("This was my this pointer%x \ n", this);
}
};

Another use of 2.this is to invoke another constructor of the current object;

3. A parameter name in your method has the same name as a member of the current object, so you need to explicitly use the This keyword to indicate that you want to use a member by using the "this. Member name" instead of the one that is the formal parameter without this.

4.this after the argument is called the current class has the same parameters of the constructor
Class person{
public static void Prt (String s) {
System.out.println (s);
}
Person () {
PRT (A person.);
}
Person (String name) {
PRT (A person name Is+name);
}
}
Public class Chinese extends person{
Chinese () {
Super (); Calling the parent class constructor (1)
PRT (A Chinese.); (4)
}
Chinese (String name) {
Super (name); Calling a constructor with the same formal parameters as the parent class (2)
PRT (his name is+name);
}
Chinese (String Name,int age) {
This (name); Call the constructor of the current class with the same parameters (3)
PRT (His-age is+age);
}
public static void Main (string[] args) {
Chinese cn=new Chinese ();
Cn=new Chinese (Kevin);
Cn=new Chinese (kevin,22);
}
}

In the constructor, this is used to qualify a member that is hidden by the same name, for example:
Class employee{
Public Employee (string name, string alias) {
THIS.name = name;
This.alias = alias;
}
}

This is also used when passing objects as arguments to other methods, for example:
Calctax (this);

This is even more necessary when declaring an indexer, such as:
public int this [int param]{
get {return array[param];}
set {Array[param] = value;}
}

Static Statics Method Usage:
In general, defining a method in a class is static, which means that you do not need an object of this class to call this method. Because this is a reference, which object invokes the method to reference which object. A static method belongs to the entire class, and this refers to a reference to the current object D. As shown below:
Class simple{
static void Go () {
System.out.println (Go ...);
}
}
public class cal{
public static void Main (string[] args) {
Simple.go ();
}
}
Invoking a static method is "class name. Method Name", and the use of static methods is simple as shown above. In general, static methods are often used by other classes in the application, and in Java's class libraries a large number of static methods are defined for this purpose.

The this pointer when c++--inherits

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.