-3

Coming from a Java background, I've got used to creating an object and using it's methods in main class by referencing the object and it's method, for example:

object.objectMethod();

Having different classes with identical-named methods wasn't an issue, but now I'm doing a project in Arduino which is pretty much C++. A tutorial on using classes in Arduino suggests using class name and double colons in .cpp file before every single method. A bit of googling led me to believe that this is called a namespace. Further googling on namespaces in C++ yielded various options of use of namespaces, but none of them were like the one in the Arduino tutorial and that got me puzzled.

My question: what is the appropriate use of namespaces and if using it as in linked tutorial is a good practice?

Justin
  • 533
  • 2
  • 6
  • 16
  • 4
    Might be a namespace, might be the name of the class the function is a member of. Might be a good idea to consult a C++ text book. –  Apr 17 '17 at 18:22
  • 3
    Java and C++ are fundamentally different despite their superficial syntactical similarities. Don’t try to emulate Java programming style in C++, it leads to terrible results. – Konrad Rudolph Apr 17 '17 at 18:22
  • 3
    Reminder: C++ allows for *free standing* function. Not everything has to be in a class. – Thomas Matthews Apr 17 '17 at 18:22
  • 1
    As far as I can see the tutorial you linked doesn't use namespaces. The `::` in there just mean that the functions belong to the class. – nwp Apr 17 '17 at 18:24
  • @ThomasMatthews, since I'm dealing with objects (sensors, I/O) I figured that it would be logical to go with objective programming and put everything in separate "bins". – Justin Apr 17 '17 at 18:25
  • 2
    In fact, a quick look at the tutorial reveals " First is the Morse:: before the name of the function. This says that the function is part of the Morse class." –  Apr 17 '17 at 18:28
  • @NeilButterworth, so if I understand it correctly, by using `::` I'm just telling the program that the function belongs to that class and nothing more? If I have more classes that have identically named functions, I still have to use namespaces? – Justin Apr 17 '17 at 18:36
  • 2
    You never have to create namespaces; they are a convenience feature. If you want to do OOP (which you don't have to) then you need to use classes. If you use classes, and to classes A and B have a function called f(), then the full names of the two functions are A::f() and B::f(). And as I said READ A C++ TEXT BOOK - youwill not learn to use the language without one. –  Apr 17 '17 at 18:41

1 Answers1

3

When you see :: in C++, it doesn't mean namespace. :: is the scope resolution operator. There are different ways to define scope in C++, Namespaces and Classes are two of these ways. Explaining their differences, pros, and cons is outside the scope if your original question.

To answer what you're asking in a more roundabout way, yes, putting functions inside Classes and Namespaces is Good C++. Otherwise, your functions will all wind up in the Global scope. Which makes for very messy programming and defeats much of the design of C++.

However, simply writing Foo::Baz() does not define Foo as a class or namespace either.

To properly use a namespace, one would first declare it and then declare the member functions as such:

namespace MyCoolNamespace {
    void aFunction();
    void anotherFunction(bool b);
}

And then define the functions like so:

void MyCoolNamespace::aFunction()
{
    /* something this function does */
}

It can then be called the way the tutorial mentions (however keep in mind, the tutorial's scope is a class, not a namespace!)

MyCoolNamespace::aFunction();

or called with the assistance of the using the using keyword:

using namespace MyCoolNamespace;

aFunction();
Cinder Biscuits
  • 4,880
  • 31
  • 51