Pass class method to object

Can I somehow pass class method to aggregated object?
I want to run one method inside Hero object too aswell.

1
2
3
4
5
6
7
8
9
10
11
12
class Menu
{
    public:
        Menu();
        virtual ~Menu();
        void displaySomeText() const;

    protected:

    private:
        Hero *hero;
};

I want to use displaySomeText() inside hero class too.
I'm not sure I'm following what you mean. If you want the Hero class to be able to call displaySomeText() then you can simply make that class take a Menu object or have displaySomeText() be a static member function if it doesn't change the state of class instances.

Also, you are not storing an "aggregated object" currently. You are storing a pointer to a Hero. This pointer needs to be initialized in the constructor or needs to be set to a Hero object at some point for it to actually reference an object. In that call, you can pass in the "this" object to a custom Hero constructor that sets its internal Menu object so it can call displaySomeText.
Last edited on
i've got displaySomeText() const; which i want to use in two different classes, but i don't want to use inheritance, is it possible any other way or only the static member?
As I said, you can either make it a static member OR you can create a Hero constructor that takes a Menu object as a parameter and use that internal aggregated object as a method to access the displaySomeText() method.

Two options:
1.) Static method
2.) Take Menu object as a parameter in Hero constructor.

The displaySomeText() method BELONGS to Menu, so you need a Menu object to be able to access it. If you don't want to inherit, then use one of the two above options.
Last edited on
OP did not specificall request access in constructor. Functions take parameters too:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Menu;

class Hero
{
public:
        void foo( const Menu& );
};

class Menu
{
public:
        void displaySomeText() const;
        void bar();
private:
        Hero hero;
};

void Hero::foo( const Menu& m ) {
    m.displaySomeText();
}

void Menu::bar() {
    hero.foo( *this );
}
i've got displaySomeText() const; which i want to use in two different classes
Does displaySomeText() access member data in both class Menu and class Hero?

If it accesses neither, then it should be a global function, not a class member.

If it accesses data from one class or the other then it should be a method of that class.

If it accesses data from both classes (and you don't want to use inheritance) then make
it a global function that takes instances of both classes as parameters. For convenience, you could give each class a helper method that calls it:

1
2
3
4
5
6
7
8
9
10
11
12
void displaySomeText(Menu &, Hero &);

class Menu {
...
    void displaySomeText() { ::displaySomeText(*this, *hero); }
    Hero *hero;
};

class Hero {
...
   void displaySomeText(Menu &m) { ::displaySomeText(m, *this); }
};
Topic archived. No new replies allowed.