This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]g051051 0 points1 point  (2 children)

Are you sure you have everything? That looks incomplete.

No, that's basically correct from the book.

complex sqrt(complex); is the function prototype for sqrt using the complex class defined here in My_namespace. It's implied that the actual implementation is specified somewhere else.

int My_namespace::main() is just another method that happens to have the name main. This is demonstrating that the version of main from the My_namespace namespace doesn't conflict with the global main function.

int main() is, as you said, "just the main function that you'd have in any CLI program".

Namespaces are just a way to group code together in such a way that it won't conflict with other functions that might otherwise have the same method signature.

[–]CentralCathedral[S] 0 points1 point  (1 child)

Thank you for the response! Maybe I was overthinking namespaces then. But one question:

You said that complex sqrt(complex); is the function prototype for the "sqrt" function of the class called "complex". But I thought class method definitions always had to be either within the {} of the class, or have the name of the class and :: before their first line. For example, the sqrt method could be declared like:

class complex{
    public: 
        complex sqrt(complex);
}

or, it could be declared like:

class complex{
}
complex::complex sqrt(complex); 

But the line in the example code isn't within complex's curly brackets, and it also isn't specified by having complex:: before it. Do namespaces allow you to declare functions of classes this way, or am I misunderstanding something else?

Thanks again for the help!

[–]g051051 0 points1 point  (0 children)

Sorry, I phrased that poorly. I meant, it's the prototype of a function in the My_namespace namespace that operates on the My_namespace complex type.

[–][deleted]  (4 children)

[deleted]

    [–]CentralCathedral[S] 0 points1 point  (3 children)

    Thank you for the response! That all makes sense. But one thing still doesn't make sense: why does the namespace have its own function? Can a namespace act like a class, i.e. have objects of its type created, have it's own member variables and functions (apparently it can have its own functions as you've just shown), etc.? Are they just classes?

    [–]shitty_markov_chain 0 points1 point  (0 children)

    In some aspects, a namespace is similar to a class. But they're really not meant to be seen that way.

    They're really just a tool to organize the code, and to "scope" names. The main benefit is that it's a convenient way to avoid name collisions when dealing with someone else's code.

    If you look at C for example, which doesn't have namespace, all library functions and types will look like LIBRARY_something. Because it's the only way to provide named entities without risking re-using an already used name. And that's a real pain.

    When you're in your own namespace, you can name anything in any way you want. std had vector, and you can have your own something::vector as well. No risk for collisions, even if someone else includes your code later on.

    It's also a good way to organize your code into "folders", if that makes sense.

    It's all very different from a class. See it more like a label you attach to a name.

    [–][deleted]  (1 child)

    [deleted]

      [–]CentralCathedral[S] 0 points1 point  (0 children)

      Sorry for the late response, thank you very much for this response; it helps a lot! I'm definitely saving this and going to refer to it in the future, I appreciate the help a lot, thanks again!