DEV Community

Jeff Qi
Jeff Qi

Posted on • Edited on

C++ tips

Projects and solutions
Projects: An container to hold every necessary component of a certain program. All files are stored in the relative project folder, including auxiliary data files, source file.
Solutions: An container to hold multiple of projects. An mechanism to integrate 1 or more programs and other resources together. Solution will store many project-related folders. Adding more projects in 1 Solution is possible but in most cases we add project which only has relationships with other projects in Solution.
Console application

  1. Precompile head file. Application wizard will generate head file 'stdafx.h' including precompile declarations(#include ...)

  2. Program using Unicode, add
    #define _UNICODE
    #define UNICODE
    in stdafx.h
    instead of main(), using wmain() if you about to use _UNICODE

type of variable: auto
'auto' is a kind of type that can be deduced from the initial value that has been assigned to the variable. Obviously a initial value is necessary if you about to use 'auto'.
auto shall not use with initialization list in the same time:

auto n{16};
which is wrong.

the real type of n in the statement is std::initializer_list

auto n(16);
is of no problem

define type: typeid
get the type of a certain expression. typeid(expression).name()

how to get global variables in a code block?
scope resolution operator

cout<<::global variable name
Enter fullscreen mode Exit fullscreen mode
pointer
  • pointer for multi-dimension array set a pointer address to the 1st element of a same dimension array
double beans[3][4];
double* pbeans;
pbeans=&beans[0][0]; 
Enter fullscreen mode Exit fullscreen mode

set a pointer address to row

double beans[3][4];
//double* pbeans; error because beans is a 2d matrix address, and pbeans type is double*;
double (*pbeans[4]); //pbeans type is pointer of a 4-element array;

pbeans= beans; 
Enter fullscreen mode Exit fullscreen mode
  • function declaration (prototype)
  1. To declare a function when using it in the source file happens before the definition.
  2. Using a more meaningful form argument in the declaration to make a better understanding of the function. When call of the function use a simple version of argument.
  • transfer array name a 2D array beans[3][4] is defined
  1. transfer double* type as argument
void print_array(double a[], int r, int c)
{
    int num{ 0 };
    for (int i{}; i < r; i++)
    {
        for (int j{}; j < c; j++)
        {
            cout << setw(6) << *(a + num++);
        }
        cout << endl;
    }
}
Enter fullscreen mode Exit fullscreen mode

call of:
print_array(beans[0],3,4);

  1. transfer double*[4] as argument
void yield(double a[][4], int n)
{
    for (int i{}; i < n; i++)
    {
        for (int j{}; j < 4; j++)
        {
            cout << setw(5) << a[i][j];
        }
        cout << endl;
    }
}
Enter fullscreen mode Exit fullscreen mode

call of:
yield(beans, _countof(beans));

reference

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now