C++ 作用域解析运算符 vs this指针

C++ 作用域解析运算符 vs this指针

作用域解析指针和this指针对于实现有效的C++程序并在软件开发敏捷生命周期方法论期间设计面向对象的过程至关重要。作用域解析运算符帮助开发者访问类成员或静态实体。另一方面,“this”指针帮助我们处理具有相同名称的本地对象成员变量。

C++代码

// Here we are writing down the C++ programming language code to demonstrate
// the concept of Scope Resolution Operator vs this pointer in C++
// where the code shows the parameters which are local and hides the 
// class members inside it!
#include 
using namespace std;

// the below code snippet helps us with creating the test class  
class Test {
    int a;

public:
    Test() { a = 1; }

    // here, the parameter 'a', which is local in nature, helps us with
    // hiding the member class 'a'!
    void func(int a) { cout << a; }
};

// The main driver code functionality starts from here. It helps us to 
// implement the concept Scope Resolution Operator vs this pointer in C++
int main()
{
    // this is the sample test object which we have implemented from class
    Test obj;
    int k = 3;
    obj.func(k);
    return 0;
    // end of the main driver code functionality
}

输出:

3

C++代码

// Here we are writing down the C++ programming language code to demonstrate
// the concept of Scope Resolution Operator vs this pointer in C++
// where the code shows the parameters which are local and hides the 
// class members inside it!
#include 
using namespace std;
// the below code snippet helps us with creating the test class  
class Test {
    int a;

public:
    Test() { a = 1; }

    // here, the parameter 'a', which is local in nature, helps us with
    // hiding the member class 'a'!

    void func(int a) { cout << this->a; }
};

// The main driver code functionality starts from here. It helps us to 
// implement the concept Scope Resolution Operator vs this pointer in C++
int main()
{
    // this is the sample test object which we have implemented from class
    Test obj;
    int k = 3;
    obj.func(k);
    return 0;
    // end of the main driver code functionality
}

输出:

1

C++代码

// Here we are writing down the C++ programming language code to demonstrate
// the concept of Scope Resolution Operator vs this pointer in C++
// where the code shows the parameters which are local and hides the 
// class members inside it! Also, show the local variables' access with 
// the same name

#include 
using namespace std;
// the below code snippet helps us with creating the test class  
class Test {
    static int a;

public:
    // here, the parameter 'a', which is local in nature, helps us with
    // hiding the member class 'a'! but it can be accessed using '::.''
    void func(int a) { cout << Test::a; }
};

// In C++ programming language, the static members must be explicitly 
// defined
// like the format given below!

int Test::a = 1;

// The main driver code functionality starts from here. It helps us to 
// implement the concept Scope Resolution Operator vs this pointer in C++
int main()
{
    // this is the sample test object which we have implemented from class
    Test obj;
    int k = 3;
    obj.func(k);
    return 0;
    // end of the main driver code functionality
}

输出:

1

C++代码

// Here we are writing down the C++ programming language code to demonstrate
// the concept of Scope Resolution Operator vs this pointer in C++
// where the code shows the parameters which are local and hides the 
// class members inside it!
#include
using namespace std;
// the below code snippet helps us with creating the AB class
class AB {
   int x;
   public:
      AB() {
         x = 6;
      }
// here, the parameter 'x', which is local in nature, helps us with
    // hiding the member class 'x'!

   void print(int x) {
      cout<<"Hey! coder the number required is: " << this->x;
   }
};
// The main driver code functionality starts from here. It helps us to 
// implement the concept Scope Resolution Operator vs this pointer in C++

int main() {
    // this is the sample AB object which we have implemented from class
   AB ob;
   int m = 7 ;
   ob.print(m);
   return 0;
   // end of the main driver code functionality
}

输出:

Hey! Coder, the number required is: 6

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程