Different keywords

suggest change
void C++
  1. When used as a function return type, the void keyword specifies that the function does not return a value. When used for a function’s parameter list, void specifies that the function takes no parameters. When used in the declaration of a pointer, void specifies that the pointer is “universal.”
  2. If a pointer’s type is void *, the pointer can point to any variable that is not declared with the const or volatile keyword. A void pointer cannot be dereferenced unless it is cast to another type. A void pointer can be converted into any other type of data pointer.
  3. A void pointer can point to a function, but not to a class member in C++.
void vobject;   // C2182  
void *pv;   // okay  
int *pint; int i;  
int main() {  
pv = &i;  
   // Cast optional in C required in C++  
pint = (int *)pv;
Volatile C++
  1. A type qualifier that you can use to declare that an object can be modified in the program by the hardware.
volatile declarator ;
virtual C++
  1. The virtual keyword declares a virtual function or a virtual base class.
virtual [type-specifiers] member-function-declarator  
virtual [access-specifier] base-class-name

Parameters

  1. type-specifiers Specifies the return type of the virtual member function.
  2. member-function-declarator Declares a member function.
  3. access-specifier Defines the level of access to the base class, public, protected or private. Can appear before or after the virtual keyword.
  4. base-class-name Identifies a previously declared class type
this pointer
  1. The this pointer is a pointer accessible only within the nonstatic member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.
this->member-identifier

An object’s this pointer is not part of the object itself; it is not reflected in the result of a sizeof statement on the object. Instead, when a nonstatic member function is called for an object, the address of the object is passed by the compiler as a hidden argument to the function. For example, the following function call:

myDate.setMonth( 3 );  

can be interpreted this way:

setMonth( &myDate, 3 );  

The object's address is available from within the member function as the this pointer. Most uses of this are implicit. It is legal, though unnecessary, to explicitly use this when referring to members of the class. For example:

void Date::setMonth( int mn )  
{  
   month = mn;            // These three statements  
   this->month = mn;      // are equivalent  
   (*this).month = mn;  
}  

The expression *this is commonly used to return the current object from a member function:

return *this;  

The this pointer is also used to guard against self-reference:

if (&Object != this) {  
// do not execute in cases of self-reference
try, throw, and catch Statements (C++)
  1. To implement exception handling in C++, you use try, throw, and catch expressions.
  2. First, use a try block to enclose one or more statements that might throw an exception.
  3. A throw expression signals that an exceptional condition—often, an error—has occurred in a try block. You can use an object of any type as the operand of a throw expression. Typically, this object is used to communicate information about the error. In most cases, we recommend that you use the std::exception class or one of the derived classes that are defined in the standard library. If one of those is not appropriate, we recommend that you derive your own exception class from std::exception.
  4. To handle exceptions that may be thrown, implement one or more catch blocks immediately following a try block. Each catch block specifies the type of exception it can handle.
MyData md;  
try {  
// Code that could throw an exception  
md = GetNetworkResource();  
}  
catch (const networkIOException& e) {  
// Code that executes when an exception of type  
// networkIOException is thrown in the try block  
// ...  
// Log error message in the exception object  
cerr << e.what();  
}  
catch (const myDataFormatException& e) {  
// Code that handles another exception type  
// ...  
cerr << e.what();  
}  

// The following syntax shows a throw expression  
MyData GetNetworkResource()  
{  
// ...  
if (IOSuccess == false)  
  throw networkIOException("Unable to connect");  
// ...  
if (readError)  
  throw myDataFormatException("Format error");   
// ...  
}
The code after the try clause is the guarded section of code. The throw expression throws—that is, raises—an exception. The code block after the catch clause is the exception handler. This is the handler that catches the exception that’s thrown if the types in the throw and catch expressions are compatible.
try {  
throw CSomeOtherException();  
}  
catch(...) {  
// Catch all exceptions – dangerous!!!  
// Respond (perhaps only partially) to the exception, then  
// re-throw to pass the exception to some other handler  
// ...  
throw;  
}
friend (C++)
  1. In some circumstances, it is more convenient to grant member-level access to functions that are not members of a class or to all members in a separate class. Only the class implementer can declare who its friends are. A function or class cannot declare itself as a friend of any class. In a class definition, use the friend keyword and the name of a non-member function or other class to grant it access to the private and protected members of your class. In a template definition, a type parameter can be declared as a friend.
  2. If you declare a friend function that was not previously declared, that function is exported to the enclosing nonclass scope.
class friend F  
friend F;
class ForwardDeclared;// Class name is known.  
class HasFriends  
{  
   friend int ForwardDeclared::IsAFriend();// C2039 error expected  
};
friend functions
  1. A friend function is a function that is not a member of a class but has access to the class’s private and protected members.Friend functions are not considered class members; they are normal external functions that are given special access privileges.
  2. Friends are not in the class’s scope, and they are not called using the member-selection operators (. and –>) unless they are members of another class.
  3. A friend function is declared by the class that is granting access. The friend declaration can be placed anywhere in the class declaration. It is not affected by the access control keywords.
#include <iostream>  

using namespace std;  
class Point  
{  
    friend void ChangePrivate( Point & );  
public:  
    Point( void ) : m_i(0) {}  
    void PrintPrivate( void ){cout << m_i << endl; }  

private:  
int m_i;  
};  

void ChangePrivate ( Point &i ) { i.m_i++; }  

int main()  
{  
   Point sPoint;  
   sPoint.PrintPrivate();  
   ChangePrivate(sPoint);  
   sPoint.PrintPrivate();  
    // Output: 0  
           1  
}

Class members as friends

class B;  

class A {  
public:  
   int Func1( B& b );  

private:  
   int Func2( B& b );  
};  

class B {  
private:
	int _b;  

   // A::Func1 is a friend function to class B  
   // so A::Func1 has access to all members of B  
   friend int A::Func1( B& );  
};  

int A::Func1( B& b ) { return b._b; }   // OK  
int A::Func2( B& b ) { return b._b; }   // C2248

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents