Home > Blockchain >  what is "using" for? (in C )
what is "using" for? (in C )

Time:04-25

I recently have seen some codes for "using" and then I confused because of I don't know their syntax exactly.

1.

using callable_type = void(__stdcall*)(std::string);
auto Various_native::call_me(callable_type callable)
{
    callable("--- from native ---");
}
    using ST_CHAR   = char;
using namespace System;

CodePudding user response:

In the first two examples "using" plays a role similar to the older "typedef" keyword; it allows you to give a custom name/alias to an existing type, so that in latter code you can use that name (e.g. callable_type) instead of the original name (e.g. void(__stdcall*)(std::string)). That has the advantage of potentially making your code easier to read and/or write, and also the advantage that if you ever decide to change the code to use a different type instead, you only have to modify a single line of code rather than every piece of code that mentions the original type.

The third example (using namespace System;) tells the compiler to automatically look inside the System namespace for identifiers, if no namespace is explicitly specified. So for example if there is a function foo() that was declared inside the System namepace, code after the using namespace System; declaration can simply call foo() rather than having to spell out System::foo() every time.

CodePudding user response:

Have a look at this: https://en.cppreference.com/w/cpp/keyword/using

Here are the ways to use using

/* clasic use: as an improved options to the c keyword typedef 
for creating aliases for typenames in the local namespace */
using fullname = std::pair<std::string, std::string>;
using relavently_named_type = std::pair<std::pair<std::string, int>, float>;

fullname name{ "mark", "thompson" };
relavently_named_type variable_name{ { "text", 1 }, 1.0f };
/* importing named symbols to the local namespace */
using std::string, std::array, std::begin, std::end;

array character_array{ 'h','e','l','l','o',' ','w','o','r','l','d', };
string hello_world_string{ begin(character_array), end(character_array) };
std::cout << hello_world_string << '\n';
/* importing all names in a namespace into the local namespace */
using namespace std;

array character_array{ 'h','e','l','l','o',' ','w','o','r','l','d', };
string hello_world_string{ begin(character_array), end(character_array) };
std::cout << hello_world_string << '\n';
/* importing all names from an enum class/struct into the local namespace
(only since c  20) */
enum class material { wood, stone, iron, bronze, glass };
auto without_using = material::wood;

using enum material;

auto with_using = wood;
/* import to other visability from parent class(es) and/or 
specify which version of "function" to use when refering to C::function */

class A
{
private:
  int function();
};

class B
{
private:
  int function();
};

class C : A, B
{
public:
  using B::function;
};

Cppreference goes into far more detail but this covers the basic. Your question isn't phrased particulary well but i hope i gave the information you wanted. Generally if you're curios about language features and syntax you can find it on cppreference as long as you know the name. Especially look at their examples.

  • Related