6

I'm thinking this is possible, but the compiler is complaining it cannot access the protected/private members of my class. I've tried moving stuff around and changing signatures, but can't find a combination that works.

I essentially have:

class MyClass
{
public:
    friend int main(int argc, char** argv);

private:
    void test()
    {
        cout << "My friend has accessed my member" << endl;
    }
};

int main(int argc, char** argv)
{
    MyClass mc;
    mc.test();
}
Jaime
  • 1,182
  • 2
  • 12
  • 29

2 Answers2

2

What you have is correct.

Works in GCC 4.3.4

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
  • ok, yea it appears to work in 4.5.1 like that as well. I wasn't telling the whole story, trying to simplify... – Jaime Dec 23 '11 at 18:00
  • Pls see my update http://stackoverflow.com/questions/8619356/how-do-i-make-main-a-friend-of-my-class-from-within-a-library – Jaime Dec 23 '11 at 18:19
2

You probably shouldn't do what you're trying to do here -- there is certainly a better way. That being said, you could try declaring the friend function in the global namespace, friend int ::main (note the use of the scope resolution operator ::).

bobbymcr
  • 23,769
  • 3
  • 56
  • 67
  • 1
    I tried this as well in my case, but didn't work. I need to provide more details to the problem... – Jaime Dec 23 '11 at 18:03
  • Pls see my update http://stackoverflow.com/questions/8619356/how-do-i-make-main-a-friend-of-my-class-from-within-a-library – Jaime Dec 23 '11 at 18:19