Question

using ::bb::cascades::Application;

#include <bb/cascades/Application>

What do these two declaration mean?

And are there any good tutorials which states the using directive/declaration deeply?Thanks.

Was it helpful?

Solution

#include is a prepocessor directive. It basically tells the prepocessor to take the given file and replace the #include line with the files content.

using on the other hand makes it possible to use names inside a namespace (structs, enums, functions) without the namespace prefix. In this case ::bb::cascades::Application will enable you to use write

Application app;

instead of

::bb::cascades::Application app;

if ::bb::cascades::Application is a default-constructible class.

"Why do I ever need to use #include?"

In order to use a function or to create an object the compiler must know the structure of this things, for example the functions signature or the member and methods of a class. These things are written in header files. Lets have a look at a very simple example, where we provide some module (called module):

The module module

// MODULE_HPP
// only declarations, no code
namespace module{
    struct dummyStruct{
        void dummyMethod(char);
        char dummyMember;
    };
    double dummyFunc(double);
};
// MODULE_CPP
// actual implementation
namespace module{
    void dummyStruct::dummyMethod(char c){
        dummyMember = c;
    };
    void dummyFunc(double a){
        return a + 1;
    }
};

As you can see our module consists of a struct with a member and a method, and a simple function. Note that we wrap everything up in the namespace module. Now we have another program which wants to use module:

#include <iostream>
using module::dummyFunc;

int main(){
    std::cout << dummyFunc(1) << std::endl;
}

And this won't work, because the compiler doesn't know about both the namespace module. You need to add the declaration, which can be done by using #include (see first paragraph of this answer):

#include <iostream>
#include "module.hpp"
using module::dummyFunc;

int main(){
    std::cout << dummyFunc(1) << std::endl;
}

Note that you'll need to compile both module.cpp and main.cpp, otherwise you'll get linker errors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top