There is a code, and a couple of places where I do not quite understand:

class myclass{ public: int sum; int mum; void sum_it(int x); }; void myclass::sum_it(int x){ int i; sum = 0; for(i=x; i; i--) sum+=i; } int main() { int myclass::*dp;//1 указатель на класс??? void (myclass::*fp)(int x);//2 указатель на класс, каким образом, через что передается х???? myclass c; dp = &myclass::sum;//ссылки fp = &myclass::sum_it;// (c.*fp)(7);//А вот это я совсем плохо понял...( cout << "Summation of 7 is " << c.*dp;// c.*dp;-это тоже плохо понимаю( getch(); return 0; } 

Explain on fingers how it works ... Thank you.

  • one
    Have you tried to compile this code? - andrybak
  • Of course it works ... - Alerr
  • Now I understand, and do not quite understand (why. * And -> * generally needed ...). You can do everything without them ... - Alerr
  • I meant to compile and run to see what it does. - andrybak

2 answers 2

 #include <iostream> using namespace std; class myclass{ public: int sum; int mum; void sum_it(int x); }; void myclass::sum_it(int x){ int i; sum = 0; for(i=x; i; i--) sum+=i; } int main() { int myclass::*dp; // числовая ссылка которая сейчас никуда не указывает. void (myclass::*fp)(int x); myclass c; dp = &myclass::sum; // тут привязка dp к сумме в классе fp = &myclass::sum_it; // ссылка на функцию sum_it (c.*fp)(50); // c - класс. вызов функции sum_it через ссылку fp с передачей числа 7 cout << "Summation of 7 is " << c.*dp << endl;// c.*dp - это ссылка в сумме класса system("pause"); return 0; } 

    dp is a pointer to an int.

    dp = &myclass::sum; - assign the address myclass::sum; variable dp .

    fp is a pointer to a function that takes an int and returns nothing ( void ).

    (c.*fp)(7); - call the function pointed to by fp from argument 7 .

    As I understand it, this design allows you to store the relative address of the field / method in the class.