0

I know in c++, struct can be used as an object template, and I know that class can be used as an object template.

Can namespace be used as an Object Template?

To be clear;

MyClass myclass;
myclass.memberFunction();
MyStruct mystruct;
mystruct.memberFunction();

Can you do something along the lines of:

MyNamespace mynamespace;
mynamespace.MyClass.memberFunction();

Either by that syntax, or something similar.

Community
  • 1
  • 1
user2690449
  • 53
  • 1
  • 5
  • 4
    No. I don't know what more to say about it than that. – Wintermute Jan 15 '15 at 13:03
  • you can, I think a found an example, but I advise again it – Irrational Person Jan 15 '15 at 13:04
  • 1
    ^ established by fair MD5 of the post markdown source – sehe Jan 15 '15 at 13:06
  • 1
    Similar to this? http://stackoverflow.com/questions/12905951/why-cant-namespaces-be-template-parameters – Daerst Jan 15 '15 at 13:07
  • 1
    What is this "Object Template" terminology you've invented? – Lightness Races in Orbit Jan 15 '15 at 13:34
  • 1
    @Daerst No, I think it’s completely unrelated, and OP is simply improperly using the words “object” and “template”. – Konrad Rudolph Jan 15 '15 at 13:45
  • @LightnessRacesinOrbit http://stackoverflow.com/a/468231/2690449 – user2690449 Jan 15 '15 at 14:02
  • 6
    I just saw your (now deleted) post on [metase]. I'm going to assume good faith and suppose you don't realize what you're doing. You seem to be using rape as a metaphor for downvoting. This trivializes rape, which makes your meta post extremely offensive. This is why your meta post was extremely poorly received. Seriously, don't do that. – Gilles 'SO- stop being evil' Jan 16 '15 at 02:45
  • @Gilles Thanks for the comment `You seem to be using rape as a metaphor for downvoting.` That is incorrect. I agree that the rape metaphor was too strong; thank you for pointing that out; it was of poor taste. The metaphor however pointed to the cyber bullying and abuse received, which offered nothing constructive to the post. When I asked him to stop because he was breaking COD, he asked why, and when I told him why, he accused me of namecalling on his behalf. I did no such thing. When the comments were edited, deleting my posts, while leaving in his insults, that is when I made the metapost. – user2690449 Jan 16 '15 at 03:52

1 Answers1

2

By definition, namespace is just a namespace, it's not template or factory of anything, the closest counterpart in Java is package, so if you're thinking of creating a new instance of a namespace, it not possible and meaningless, just like you cannot create instance of package in Java.

But if you just want to give an existing namespace an alias, you can so something like:

namespace MyNamespace {
    class MyClass {
    public:
        static void MyStaticFunction(...) {...}
    };
}

namespace mynamespace=MyNamespace;
mynamespace::MyClass::MyStaticFunction(...);

Note that only static member functions can be called without a class instance.

Windoze
  • 321
  • 2
  • 13
  • Great answer. Simple, concise, and answers my question perfectly. – user2690449 Jan 15 '15 at 15:22
  • 1
    One of the important problem with using namespaces this way is that namespaces are **open** (you can close the brace and open it anew, also in another file, and it will mean "the same" namespace), in contrast to classes, which as **closed** (once you closed the brace that opened the class definition, no more definitions to this class can be added). It's not possible to define a possibility to instantiate something that relies on an entity that may be soon extended by some extra things. – Ethouris Jan 15 '15 at 15:55
  • 1
    @Ethouris, it not quite true, there are many languages out there support reopen-able classes, i.e. Categories in Objective-C, implicit methods in Scala, mixin in Ruby, class helpers in Delphi... – Windoze Jan 15 '15 at 16:04
  • I didn't mean any other language than C++. If you start talking about "other language", we can quickly finish with Tcl, in which you can remove and rename whatever is defined inside, as well as synthesize the function code and execute it in runtime. Well, not much different to other scripting languages. On the other hand, you can still rely on a base class and have things extended in the deriving class etc... – Ethouris Jan 20 '15 at 18:36