Differences between using pointer to the class member and the structure member

starkk92

I am new to c++.I was told the structures and the classes are almost the same.The main differece is classes in c++ are by default private while structures are public.

I have two different codes where we use pointers to structure members and classe members.

struct Simple { int a; };
int main() {
Simple so, *sp = &so;
sp->a;
so.a;
}

We use pointers to struct members in the above way.

Why do we have to use the pointers to class members int he following way?

class X {
public:
  int a;
  void f(int b) {
    cout << "The value of b is "<< b << endl;
  }
};

int main() {

  // declare pointer to data member
  int X::*ptiptr = &X::a;
  int X::*ptiptr1 = &X::a;

  // create an object of class type X
  X xobject,xobject1;

  // initialize data member
  xobject.*ptiptr = 10;
  xobject->*ptiptr1 = 11;

}

Why are we first declaring a pointer to the class data member? Why can't we do it like we have done for structures?

Please help me out.Thanks.

Mike Seymour

You're doing two very different things here.

The first is creating a normal object pointer to one particular object. This refers to that object, and can be used to access any of its members.

The second creates a pointer-to-member, which is very different to an object pointer. It refers to a class member and not a particular object, and can be applied to any object to access that member.

In either case, it's irrelevant whether the class is defined using the class or struct keyword. That only affects whether members default to being private or public.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Dereferencing pointer to container class member

Pointer to class data member "::*"

Function pointer to class template member

Access to structure member by pointer

accessing structure's member by using pointer

pointer to function as static member in structure

Using pointer to member function of a base class on object of derived class

Should the member of class be a pointer?

In C++ Pointer Member in class

Why is using a reference or unique pointer member of a class a bad thing?

Unable to assign the value to structure member using double pointer

Why is structure member accessed using pointer is not getting updated?

Making the member of a structure of type structure double pointer

Accessing the member of a structure by double pointer of that structure type

Can I use "using" instead of "typedef" for a pointer to class member variable?

accessing structure member character pointer

Difference between using a structure member and cast a structure pointer when "emulate" polymorphism in C

Some questions on pointer to member of class

difference between structure pointer and member of structure pointer

pointer to a member function of a class

Member function pointer on derived class

Pointer to managed class member

Pointer to class member function

Shared pointer between two classes as class member

Pointer to Member Function Class Type

Pointer to Parent Class Member Method

Returning the value of a class member pointer variable using 'this' pointer

C++ - calling a member function of another class using a pointer

Ctypes accessing structure member pointer to another structure