Pointer to Class Data Member "::*"

Pointer to class data member ::*

It's a "pointer to member" - the following code illustrates its use:

#include <iostream>
using namespace std;

class Car
{
public:
int speed;
};

int main()
{
int Car::*pSpeed = &Car::speed;

Car c1;
c1.speed = 1; // direct access
cout << "speed is " << c1.speed << endl;
c1.*pSpeed = 2; // access via pointer to member
cout << "speed is " << c1.speed << endl;
return 0;
}

As to why you would want to do that, well it gives you another level of indirection that can solve some tricky problems. But to be honest, I've never had to use them in my own code.

Edit: I can't think off-hand of a convincing use for pointers to member data. Pointer to member functions can be used in pluggable architectures, but once again producing an example in a small space defeats me. The following is my best (untested) try - an Apply function that would do some pre &post processing before applying a user-selected member function to an object:

void Apply( SomeClass * c, void (SomeClass::*func)() ) {
// do hefty pre-call processing
(c->*func)(); // call user specified function
// do hefty post-call processing
}

The parentheses around c->*func are necessary because the ->* operator has lower precedence than the function call operator.

Pointer to different components of a class

#include<iostream>
#include<math.h>
using namespace std;
class Rectangle
{
int a,b;
public:
};
class Perimeter : public Rectangle
{
public:
int c;
void P(int a, int b)
{
c = 2 * (a + b);
cout<<"This Is The Perimeter Of The Rectangle: "<<c<<endl;
}
};
class Area : public Rectangle
{
public:
int c;
void A(int a, int b)
{
c = a * b;
cout<<"This Is The Area Of The Rectangle: "<<c<<endl;
}
};
class Diagonal : public Rectangle
{
public:
float c;
void D(int a, int b)
{
c = sqrt((a*a)+(b*b));
cout<<"This Is The Diagonal Of Rectangle: "<<c<<endl;
}
};
int main()
{
int e,f;
cout<<"Enter Length And Breadth: "<<endl;
cin>>e>>f;
/***************************************/
Perimeter p; //CREATING AN OBJECT
Perimeter *Peri; //CREATING A POINTER TO THE OBJECT
Peri=&p; //ASSIGNING ADDRESS TO THE POINTER
Peri->P(e,f); //MEMBER ACCESS USING POINTER TO AN OBJECT
/**************************************/
Area a;
int Area::*ptr=&Area::c; //CREATING A POINTER TO THE DATA MEMBER
a.*ptr = e;
a.A(e,f);
/*************************************/
Diagonal d;
void (Diagonal::*Dia)(int,int)=&Diagonal::D; //CREATING POINTER TO MEMBER FUNCTION
(d.*Dia)(e,f); //THIS IS HOW WE CALL THE MEMBER FUNCTION USING ITS POINTER
/*************************************/
return 0;
}

I believe this is what you were looking for.

there are some errors you made in the program. i didn't correct them but i am pointing them out.
though you didn't write anything(create any functions) in the parent class, creating pointer to an object of the sub-class is useless. in this case, early binding is taking place. you can go with a pure virtual function following function Over-Riding.

Can a nested class have a member pointer to the outer class?

You can declare a pointer or reference to the outer class since it doesn't require a complete type. Declaring an object would instead require the type to be complete. See reference on incomplete types

This is the same reason why you can have a pointer to the same class as a data member:

class Outer
{
Outer* p; // Outer is an incomplete type at this point
};

Using pointers to access object data members in an array

Your pointer syntax for assigning to the array element is correct:

*(arrayPtr + monkeyMarker) = Monkey();

Your syntax for accessing it is wrong because of operator precedence. . has higher precedence than *, so

*(arrayPtr + monkeyMarker).getAge

is treated as

*((arrayPtr + monkeyMarker).getAge)

which is trying to dereference the getAge function pointer.

You need to add parentheses. Also, since getAge is a function, you need to call it with ().

(*(arrayPtr + monkeyMarker)).getAge()

You can simplify this using the -> operator to indirect through a pointer:

(arrayPtr + monkeyMarker)->getAge()

How to access pointer data member using class object in C++?

When you assign bellow statement :

temp.num = *num + obj.getF();

Actually you assign a float number to a float pointer!

So, use of bellow :

(*temp.num) = (*num) + obj.getF();

Instead of :

temp.num = *num + obj.getF();

(C++) How to write a clone method for a class which has a unique pointer as a data member?

Simply add a copy constructor to DecoratedDerived that clone()'s the data member, eg:

class DecoratedDerived : public Base {
private:
unique_ptr<Base> ptr;
// ...
public:
DecoratedDerived(const DecoratedDerived &src)
: Base(src), ptr(src.ptr ? src.ptr->clone() : nullptr)
{
}

// ...

DecoratedDerived* clone() const override{
return new DecoratedDerived(*this);
}
};

Online Demo

Understanding pointer to member of class of type - no polymorphism

A pointer-to-data-member is normally represented by a simple integer value, telling the offset of the beginning of the owner class to the beginning of the member. So the algorithm of retrieving a data member given a pointer to its owner is as simple as "add the offset to the address and dereference".

However, in order to go from a pointer-to-derived to a pointer-to-base, such a simple description is not enough. The offset is not necessarily constant, because of virtual inheritance. In order to find the offset, we need an actual derived object. The offset is stored in there somewhere. So in the general case, a pointer-to-data-member would have to be represented as a combination of at least two offsets, and fetching the member would require a decision (is a virtual base present?)

I guess the standard could have provided such conversion for the non-virtual inheritance case only. In this case the offset is constant and the conversion would consist of adding the two offsets (unless I'm missing some other corner case). However this is not being done, and the reason is, of course, that no one felt motivated enough to write a proposal. Pointers to data members (as opposed to pointers to member functions) were originally considered by Stroustrup "an artifact of generalization rather than something genuinely useful" (D&E, 13.11 Pointers to Members). They are now being used (mostly) to describe class layouts in an implementation-independent way, but there is no real need to use them polymorphically.



Related Topics



Leave a reply



Submit