Friends in namespaces



  • Hi
    I have been experimenting with friends and have come across something that I cannot explain

    If I declare a class as
    @
    class MyClass: public QObject
    {
    Q_OBJECT

    public:
    friend int main(int,char**);

    private:
    MyClass(QObject *parent = 0);

    };
    @

    Then I can instantiate a MyClass in main -
    @
    int main(int argc, char argv[])
    {
    QApplication a(argc, argv);
    MyClass
    = new MyClass;
    raiigui w;
    w.show();
    return a.exec();
    }
    @

    However, if I place MyClass inside a namespace -
    @
    namespace MyNamespace
    {
    class MyClass: public QObject
    {
    Q_OBJECT

    public:
    friend int main(int,char**);

    private:
    MyClass(QObject *parent = 0);
    };
    }@

    I cannot now instantiate MyClass
    @
    MyNamespace::MyClass mc = new MyNamespace::MyClass;
    @

    Could anybody explain why this is?

    Thanks


  • Lifetime Qt Champion

    Hi,

    AFAIK you are friending

    @int MyNameSpace::main(int, char**)@

    And not

    @int main(int, char**)@

    Out of curiosity, what's your purpose with that friending ?



  • Hi Thanks for your reply

    Placing main inside the same namespace still wont compile!

    I am investigating different ways of implementing singletons


  • Lifetime Qt Champion

    You can't have main in a namespace

    From the c++ standard:
    [quote]
    3.6.1 Main function [basic.start.main]
    1 A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function. [ Note: In a freestanding environment, start-up and termination is implementation-defined; start- up contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. — end note ]
    [/quote]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.