-6

I am trying to understand delegation in c++. I read that "delegation is pointer to function", and i saw several examples, but unfortunately I cant get it. I have created code to try, because I thought that maybe while programming i will understand it. Unfortunately I didn't.

#include <iostream>
using namespace std;

class person{
    private: 
        int age;
    public:
        person(age){
            this->age = age;
        }

        // virtual void changeAge(int arg) = 0;
};

class addNumber {
    public:
        int changeAge(int arg) {
            arg += arg+1; 
        }
};

int main(){
    person Olaf;
}

So based on this source I tried:

Olaf = &addNumber::changeAge(10);

or

addNumber test;

Olaf = &addNumber::changeAge(10);

Both does not work. That means program is not compiling. I want to make person object to use changeName of addNumber class method to change the age of instance person class.

Ubunnn
  • 21
  • 3
  • 1
    *"... I read that "delegation is pointer to function"... "* - Where did you read that? – WhiZTiM Dec 18 '17 at 13:10
  • 1
    What is your question? – EvilTeach Dec 18 '17 at 13:10
  • Please define what do you mean by "_Both does not work._" – Algirdas Preidžius Dec 18 '17 at 13:10
  • Your `changeAge` method needs to either static or you need to create an instance of addNumber. – Jake Freeman Dec 18 '17 at 13:13
  • 1
    it is completely unclear what the line `Olaf = &addNumber::changeAge(10);` is supposed to do. `Olaf` is a `person` while the rhs is most likely supposed to be a function pointer (it isnt). How is that assignment supposed to work? – 463035818_is_not_an_ai Dec 18 '17 at 13:13
  • 1
    You are trying to assign an address of value returned by function call to an object of unrelated type `person`. This has nothing to do with pointers to functions or delegates. You should get familiar with [C++ syntax](http://en.cppreference.com/w/cpp/language/pointer) first. – user7860670 Dec 18 '17 at 13:13
  • I am trying to force 'Olaf' to use `changeAge` metod of `addNumber` class. I don't want to use inheritance, because I want to learn delegation. – Ubunnn Dec 18 '17 at 13:33

2 Answers2

2

In C++11 and later you have closures (e.g. thru std::function etc...) and lambda expressions (that is, anonymous functions)

But you don't exactly have delegation in C++, even if you also have pointers to functions and pointers to member functions. But closures and lambda expressions are nearly equivalent, in power of expression, to delegation.

You should read SICP then some good C++ programming book to understand these notions.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
2

First, let's use a typedef for the function:

typedef int agechanger(int);

this makes a new type, agechanger, which will be used in code for passing the function instances around.

Now, you should give your person class a proper constructor, and properly incapsulate the age field providing a public getter. Then add a method that accepts a function as argument, function of type agechanger, of course.

class person
{
private:
    int age;
public:
    person(int age){
        this->age = age;
    }

    int getAge() const {
        return age;
    }
    void changeAge(agechanger f)
    {
        age = f(age);
    }
};

Then define a function that fits our type, inside a class:

class addNumber {
public:
    static int changeAge(int arg) {
        return arg + 1;
    }
};

Notice that the function is marked as static and returns the passed int incremented by one.

Let's test everything in a main:

int main()
{
    person Olaf(100); //instance of person, the old Olaf

    Olaf.changeAge(addNumber::changeAge); //pass the function to the person method

    std::cout << Olaf.getAge() << std::endl; //Olaf should be even older, now
}

Let's make and use a different function, ouside a class, this time:

int younger(int age)
{
    return age -10;
}

int main(){

    person Olaf(100);

    Olaf.changeAge(younger);

    std::cout << Olaf.getAge() << std::endl; // Olaf is much younger now!
}

I hope that having code that works is going to help you understand things better. The topic you're asking about, here, is generally considered advanced, while I think you should review some more basic topics of c++, first (functions and classes, for example).

p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35