Class method as a friend of a class

Programming, for all ages and all languages.
Post Reply
synthetix
Posts: 15
Joined: Sun Jan 28, 2007 8:45 am
Location: Somewhere in the infinite dimension universe that my vast imagination is.

Class method as a friend of a class

Post by synthetix »

Hello, as I'm on the run, I have no time to explain much.

I would like a class method (only one) to be able to access private data of another class. However, if I know how to make an entire class the friend of another class, I do not know how to do it for a single method

Ex:

class ABC {
public:
sing() {
say(song)

private:
string song = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


class ABCModify {
bool ChangeSong(string NewSong);
}

I want ABCModify::ChangeSong to be the friend of class ABC

Thanks in advance for any information
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

Code: Select all

#include <stdio.h>

class ABC;
class ABCModify {
public:
    bool ChangeSong(char * NewSong);
};

class ABC { 
    friend bool ABCModify::ChangeSong(char * NewSong);

public: 
    void sing(void) { 
        printf(song);
    }

private: 
    char * song; 
};

ABC test;

bool ABCModify::ChangeSong(char * NewSong) {
    test.song = NewSong;
}

int main(int argc, char * argv[]) {
    ABCModify           x;

    x.ChangeSong("beep");

    test.sing();
}
Author of COBOS
Post Reply