c++中指向图片的指针_C ++中指向类成员的指针

本文介绍了C++中如何定义和使用指向类成员函数和数据成员的指针。通过指针可以访问和操作类的对象,语法较为复杂,通常在特殊情况下使用。记得要将成员函数和数据成员设置为公共以便于指针操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

c++中指向图片的指针

Just like pointers to normal variables and functions, we can have pointers to class member functions and member variables.

就像指向普通变量和函数的指针一样,我们可以具有指向类成员函数和成员变量的指针。

Let's see how this works.

让我们看看它是如何工作的。

定义类指针 (Defining a Pointer of Class type)

We can define pointer of class type, which can be used to point to class objects.

我们可以定义类类型的指针,该指针可用于指向类对象。

class Simple
{
    public:
    int a;
};

int main()
{
    Simple obj;
    Simple* ptr;   // Pointer of class type
    ptr = &obj;
 
    cout << obj.a;
    cout << ptr->a;  // Accessing member with pointer
}

Here you can see that we have declared a pointer of class type which points to class's object. We can access data members and member functions using pointer name with arrow -> symbol.

在这里您可以看到我们已经声明了一个指向类的对象的类类型的指针。 我们可以使用带有箭头->符号的指针名称来访问数据成员和成员函数。

指向类数据成员的指针 (Pointer to Data Members of Class)

We can use pointer to point to class's data members (Member variables).

我们可以使用指针指向类的数据成员(成员变量)。

Syntax for Declaration :

声明语法:

datatype class_name :: *pointer_name;

Syntax for Assignment:

分配语法:

pointer_name = &class_name :: datamember_name;

Both declaration and assignment can be done in a single statement too.

声明和赋值都可以在单个语句中完成。

datatype class_name::*pointer_name = &class_name::datamember_name ;

将指针与对象一起使用 (Using Pointers with Objects)

For accessing normal data members we use the dot . operator with object and -> qith pointer to object. But when we have a pointer to data member, we have to dereference that pointer to get what its pointing to, hence it becomes,

为了访问普通数据成员,我们使用点. 带有对象和-> qith指向对象的指针的运算符。 但是,当我们有一个指向数据成员的指针时,我们必须取消对该指针的引用以获取其指向的内容,因此它变成了,

Object.*pointerToMember

and with pointer to object, it can be accessed by writing,

并带有指向对象的指针,可以通过编写来访问它,

ObjectPointer->*pointerToMember

Lets take an example, to understand the complete concept.

让我们举个例子,了解完整的概念。

class Data
{
    public:
    int a;
    void print() 
    { 
        cout << "a is "<< a; 
    }
};

int main()
{
    Data d, *dp;
    dp = &d;     // pointer to object
 
    int Data::*ptr=&Data::a;   // pointer to data member 'a'

    d.*ptr=10;
    d.print();

    dp->*ptr=20;
    dp->print();
}

a is 10 a is 20

a是10 a是20

The syntax is very tough, hence they are only used under special circumstances.

语法非常严格,因此仅在特殊情况下使用。

指向类成员函数的指针 (Pointer to Member Functions of Class)

Pointers can be used to point to class's Member functions.

指针可用于指向类的成员函数。

Syntax:

句法:

return_type (class_name::*ptr_name) (argument_type) = &class_name::function_name;

Below is an example to show how we use ppointer to member functions.

下面是一个示例,说明我们如何使用ppointer成员函数。

class Data
{ 
    public:
    int f(float) 
    { 
        return 1; 
    }
};

int (Data::*fp1) (float) = &Data::f;   // Declaration and assignment
int (Data::*fp2) (float);        // Only Declaration

int main(0
{
    fp2 = &Data::f;   // Assignment inside main()
}

要记住的几点 (Some Points to remember)

  1. You can change the value and behaviour of these pointers on runtime. That means, you can point it to other member function or member variable.

    您可以在运行时更改这些指针的值和行为。 这意味着,您可以将其指向其他成员函数或成员变量。



  2. To have pointer to data member and member functions you need to make them public.

    要具有指向数据成员和成员函数的指针,您需要将其公开。

翻译自: https://www.studytonight.com/cpp/pointer-to-members.php

c++中指向图片的指针

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值