What Does the :: Mean in C++

What does the :: mean in C++?

:: is the scope resolution operator - used to qualify names. In this case it is used to separate the class AirlineTicket from the constructor AirlineTicket(), forming the qualified name AirlineTicket::AirlineTicket()

You use this whenever you need to be explicit with regards to what you're referring to. Some samples:

namespace foo {
class bar;
}
class bar;
using namespace foo;

Now you have to use the scope resolution operator to refer to a specific bar.

::foo::bar is a fully qualified name.

::bar is another fully qualified name. (:: first means "global namespace")

struct Base {
void foo();
};
struct Derived : Base {
void foo();
void bar() {
Derived::foo();
Base::foo();
}
};

This uses scope resolution to select specific versions of foo.

What does :: mean in c++?

:: is the scope operator to used to identify and specify the context that an identifier refers to.

using a very simple google search, IBM describes it as:

The :: (scope resolution) operator is used to qualify hidden names so
that you can still use them. You can use the unary scope operator if a
namespace scope or global scope name is hidden by an explicit
declaration of the same name in a block or class.

What do ^() {} and ^{} mean in C++?

It's hard to find a duplicate with ^() {} symbols, so I'll post an answer.

These are "blocks", which is a clang compiler extension that creates lambda-like closures.

More info at wiki and in clangs Language Specification for Blocks.

When there is an empty argument list, the (void) can be omitted, the ^ { recvData(data, NULL); } is the same as ^ void (void) { recvData(data, NULL); }.

What does the - operator mean in C++?

It's a shortcut for dereference followed by property access (or method invocation).

In code, here are some examples of this equivalence:

Foo *foo;

// field access
foo->bar = 10;
(*foo).bar = 10;

// method invocation
foo->baz();
(*foo).baz();

This is especially convenient when you have a long sequence of these. For example, if you have a singly linked list data structure in which each element has a pointer to the next, the following are equivalent ways of finding the fifth element (but one looks much nicer):

linked_list *head, *fifth;
fifth = head->next->next->next->next;
fifth = (*(*(*(*head).next).next).next).next;

What does the code `[&]()` mean in c++?

This is an example of a lambda function.

C++ is full of obscure corners, but as odd syntax goes, lambda functions are well worth learning. The basic syntax to declare a lambda function is []() { }. This is an expression that results in an object of an implementation-defined type that can be called as if it were a function (because, somewhere in the compiler's implementation of the lambda expression syntax, it is a function).

The first advantage that this gives is that the function can be declared inline with the rest of the code, instead of having to be declared apart (which is often inconvenient for very short or specific-use functions).

The most powerful part of lambda expressions, though, is their ability to capture variables from the surrounding context: That's what the & does in your sample code. A lambda declared with [&] captures all variables (and the implicit this pointer if used inside a method) by reference. (There's also [=] to capture by value, and even an extended syntax to capture only specific variables in specific ways.)

In short, this code:

m_time.Tick([&]() { ... });

calls the Tick method on the m_time object, and passes it a lambda function that captures the surrounding context by reference. Presumably, the Tick method then calls this lambda function during its execution. Within the ..., the variables available in the scope in which the lambda was declared can be then be used -- and this capturing ability is the most powerful and convenient feature of lambdas.

What does - mean in C++?

It's to access a member function or member variable of an object through a pointer, as opposed to a regular variable or reference.

For example: with a regular variable or reference, you use the . operator to access member functions or member variables.

std::string s = "abc";
std::cout << s.length() << std::endl;

But if you're working with a pointer, you need to use the -> operator:

std::string* s = new std::string("abc");
std::cout << s->length() << std::endl;

It can also be overloaded to perform a specific function for a certain object type. Smart pointers like shared_ptr and unique_ptr, as well as STL container iterators, overload this operator to mimic native pointer semantics.

For example:

std::map<int, int>::iterator it = mymap.begin(), end = mymap.end();
for (; it != end; ++it)
std::cout << it->first << std::endl;

What does '' mean in C?

<< is the left shift operator. It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1.

It is commonly used to create flags, numbers that can be combined together with | (bit or) and various operations can be applied to them, such as testing whether a flag is set, setting a flag, removing a flag, etc.

The reason that they can be combined together without interfering with each other is that each one is a power of two, and that is the reason for using 1 << x, because that yields powers of two:

1 << 0 == 20 == 1 == binary 0001
1 << 1 == 21 == 2 == binary 0010
1 << 2 == 22 == 4 == binary 0100
1 << 3 == 23 == 8 == binary 1000
etc

You can read about bit flags here: http://www.codeproject.com/KB/tips/Binary_Guide.aspx

What does ** do in C language?

In C arguments are passed by values. For example if you have an integer varaible in main

int main( void )
{
int x = 10;
//...

and the following function

void f( int x )
{
x = 20;
printf( "x = %d\n", x );
}

then if you call the function in main like this

f( x );

then the parameter gets the value of variable x in main. However the parameter itself occupies a different extent in memory than the argument. So any changes of the parameter in the function do not influence to the original variable in main because these changes occur in different memory extent.

So how to change the varible in main in the function?

You need to pass a reference to the variable using pointers.

In this case the function declaration will look like

void f( int *px );

and the function definition will be

void f( int *px )
{
*px = 20;
printf( "*px = %d\n", *px );
}

In this case it is the memory extent occupied by the original variable x is changed because within the function we get access to this extent using the pointer

    *px = 20;

Naturally the function must be called in main like

f( &x );

Take into account that the parameter itself that is the pointer px is as usual a local variable of the function. That is the function creates this variable and initializes it with the address of variable x.

Now let's assume that in main you declared a pointer for example the following way

int main( void )
{
int *px = malloc( sizeof( int ) );
//..

And the function defined like

void f( int *px )
{
px = malloc( sizeof( int ) );

printf( "px = %p\n", px );
}

As parameter px is a local variable assigning to it any value does not influence to the original pointer. The function changes a different extent of memory than the extent occupied by the original pointer px in main.

How to change the original pointer in the function?
Just pass it by reference!

For example

f( &px );
//...

void f( int **px )
{
*px = malloc( sizeof( int ) );

printf( "*px = %p\n", *px );
}

In this case the value stored in the original pointer will be changed within the function because the function using dereferencing access the same memory extent where the original pointer was defined.



Related Topics



Leave a reply



Submit