classes and namespaces
-
Is there any particular reason why the following code won't compile? Thats very basically what I have got. I'm only doing this as a quick hack, so I'm not at all concerned with code design, but it really intrigued me as to why it won't let me declare a namespace within a class? All the things I've read on the internet deal only with class declarations within a namespace but not visa versa. Just wondering whether this the c++ standard leaves it up to the compiler or whether its officially illegal code? Seems a bit silly to me, but maybe theres a reason for it? Just wondered if anyone's knows what it is?
class Outcome { public: enum Type { kHalt, kMove, kJump, }; Type m_type; namespace halt { void Low(void); void Medium(void); void High(void); }; namespace move { void Low(void); void Medium(void); void High(void); }; namespace jump { void Low(void); void Medium(void); void High(void); }; };
"When I left you I was but the learner, now I am the master" - Darth Vader -
Is there any particular reason why the following code won't compile? Thats very basically what I have got. I'm only doing this as a quick hack, so I'm not at all concerned with code design, but it really intrigued me as to why it won't let me declare a namespace within a class? All the things I've read on the internet deal only with class declarations within a namespace but not visa versa. Just wondering whether this the c++ standard leaves it up to the compiler or whether its officially illegal code? Seems a bit silly to me, but maybe theres a reason for it? Just wondered if anyone's knows what it is?
class Outcome { public: enum Type { kHalt, kMove, kJump, }; Type m_type; namespace halt { void Low(void); void Medium(void); void High(void); }; namespace move { void Low(void); void Medium(void); void High(void); }; namespace jump { void Low(void); void Medium(void); void High(void); }; };
"When I left you I was but the learner, now I am the master" - Darth Vader -
At what point do you get the compiler error? The line
Type m_type;
is invalid. You'd have to scope it withOutcome::Type m_type;
Bob CioraI changed it to this but I still get the same error message from gcc : "error: parse error before `namespace'" Do you think its a compiler bug or is it just not supposed to be supported? Its a bit weird don't you think considering that a class is essentially a namespace and you can have nested class declarations and nested namespace declarations, but not this way round? class Outcome { public: enum Type { kHalt, kMove, kJump, }; namespace halt { void Low(void); void Medium(void); void High(void); }; namespace move { void Low(void); void Medium(void); void High(void); }; namespace jump { void Low(void); void Medium(void); void High(void); }; Outcome::Type m_type; }; "When I left you I was but the learner, now I am the master" - Darth Vader
-
Is there any particular reason why the following code won't compile? Thats very basically what I have got. I'm only doing this as a quick hack, so I'm not at all concerned with code design, but it really intrigued me as to why it won't let me declare a namespace within a class? All the things I've read on the internet deal only with class declarations within a namespace but not visa versa. Just wondering whether this the c++ standard leaves it up to the compiler or whether its officially illegal code? Seems a bit silly to me, but maybe theres a reason for it? Just wondered if anyone's knows what it is?
class Outcome { public: enum Type { kHalt, kMove, kJump, }; Type m_type; namespace halt { void Low(void); void Medium(void); void High(void); }; namespace move { void Low(void); void Medium(void); void High(void); }; namespace jump { void Low(void); void Medium(void); void High(void); }; };
"When I left you I was but the learner, now I am the master" - Darth VaderNamespaces can contain classes, but classes cannot contain namespaces. Stability. What an interesting concept. -- Chris Maunder
-
Namespaces can contain classes, but classes cannot contain namespaces. Stability. What an interesting concept. -- Chris Maunder
I'm guessing its because namespace functions have global linkage within the namespace but by default functions in a class only have local linkage via an object? "When I left you I was but the learner, now I am the master" - Darth Vader
-
Is there any particular reason why the following code won't compile? Thats very basically what I have got. I'm only doing this as a quick hack, so I'm not at all concerned with code design, but it really intrigued me as to why it won't let me declare a namespace within a class? All the things I've read on the internet deal only with class declarations within a namespace but not visa versa. Just wondering whether this the c++ standard leaves it up to the compiler or whether its officially illegal code? Seems a bit silly to me, but maybe theres a reason for it? Just wondered if anyone's knows what it is?
class Outcome { public: enum Type { kHalt, kMove, kJump, }; Type m_type; namespace halt { void Low(void); void Medium(void); void High(void); }; namespace move { void Low(void); void Medium(void); void High(void); }; namespace jump { void Low(void); void Medium(void); void High(void); }; };
"When I left you I was but the learner, now I am the master" - Darth VaderI can think of one reason. Many classes can belong to one namespace. So by giving 'using namespace' you have access to all the classes that are contained in that namespace. If what you mentioned is possible, then by giving, let's say, 'using namespace jump', you cannot create an object of class Outcome because all members of the class does not belong to that namespace. « Superman »
-
I changed it to this but I still get the same error message from gcc : "error: parse error before `namespace'" Do you think its a compiler bug or is it just not supposed to be supported? Its a bit weird don't you think considering that a class is essentially a namespace and you can have nested class declarations and nested namespace declarations, but not this way round? class Outcome { public: enum Type { kHalt, kMove, kJump, }; namespace halt { void Low(void); void Medium(void); void High(void); }; namespace move { void Low(void); void Medium(void); void High(void); }; namespace jump { void Low(void); void Medium(void); void High(void); }; Outcome::Type m_type; }; "When I left you I was but the learner, now I am the master" - Darth Vader
-
I'm guessing its because namespace functions have global linkage within the namespace but by default functions in a class only have local linkage via an object? "When I left you I was but the learner, now I am the master" - Darth Vader
Alan Chambers wrote: functions in a class only have local linkage via an object? member functions of a class receive the
this
pointer, which allow the functions to point directly the object that is using the function. a particular point is the static member functions -> no this pointer as implicit parameter
TOXCCT >>> GEII power
[toxcct][VisualCalc]