1
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.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
Tahlil
  • 2,680
  • 6
  • 43
  • 84

1 Answers1

3

#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.

Zeta
  • 103,620
  • 13
  • 194
  • 236
  • Then why do I ever need to use include? I can do what the 'include' can do with 'using directive' right? – Tahlil Dec 17 '12 at 07:41
  • 1
    no you cannot. you can't bring into scope something that you haven't included with #include . "using" only allows you to use things from other scope without fully qualifying them. But the scope has to be visible by itself which is what you use #include for – Zeks Dec 17 '12 at 07:43
  • Can you please explain with an example :) Thanks. – Tahlil Dec 17 '12 at 07:51
  • @kalkin: Improved my answer. – Zeta Dec 17 '12 at 07:52
  • So to make the example work I need to 'include' the file where the namespace 'module' is declared right? – Tahlil Dec 17 '12 at 07:56