Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Friends in namespaces
Forum Updated to NodeBB v4.3 + New Features

Friends in namespaces

Scheduled Pinned Locked Moved C++ Gurus
4 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GrahamL
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • G Offline
        G Offline
        GrahamL
        wrote on last edited by
        #3

        Hi Thanks for your reply

        Placing main inside the same namespace still wont compile!

        I am investigating different ways of implementing singletons

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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]

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved