Friends in namespaces
-
Hi
I have been experimenting with friends and have come across something that I cannot explainIf I declare a class as
@
class MyClass: public QObject
{
Q_OBJECTpublic:
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_OBJECTpublic:
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
-
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 ?
-
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]