View on GitHub

wiki

C/C++

Data types

Syntax/general notes

Typedeffing

Using something like typedef struct usb_dev_handle usb_dev_handle in a library declares the struct, but doesn’t actually define it. This is used in situations where a library is trying to completely hide the implementation details within the header.

Callbacks

Member functions aren’t allowed, since the caller would need the full object+state.

Common naming scheme

Incrementing

Pre-increment ++i increments the value of i and evaluates to the new incremented value. Post-increment i++ increments the value of i and evaluates to the original non-incremented value. Probably generally better to do this explicitly for clarity’s sake.

Volatile variables

Headers

Stack variables vs heap

Object allocation and lifetime

Object-oriented programming

Scope

Double colon (::) - selects what class’s scope you are targeting.

Inheritance

Inheritance with : during class declaration. Inherit protected and public variables. Has an access specifier which declares how the variables/functions are incorporated into the new derived class.

Multiple inheritance is allowed (but probably use interfaces instead): class CRectangle: public CPolygon, public COutput{ }

Overloading

Basic operators are overloadable. Can create a ‘vector class’ where you would ‘add’ two classes together. Would normally fail unless you overload this w/ ‘operator’ designation.

Struct vs class

In a class definition, the default access for members and base classes is private. In a struct definition, the default is public. That is the only difference between a class and a struct, although by convention, some programmers use struct only for POD (plain old data) classes and use class for all other classes.

Static variables and functions

Templates

Constructor

Constructor gets run when the object is created. Destructor is called when the object is deleted.

Common to only specify the class variables and methods in the class definition, and then fill in the actual meat further down:

class CRectangle {
    int *width, *height;
  public:
    CRectangle (int,int);
    ~CRectangle ();
    int area () {return (*width * *height);}
};

CRectangle::CRectangle (int a, int b) {
  width = new int;
  height = new int;
  *width = a;
  *height = b;
}

Classname::Classname is a constructor, Classname::~Classname is a destructor.

Colon after constructor is an ‘initialization list’. By default, when you initialize an inherited class, all inherited constructors get called, then finally the class’s constructor is called. If you don’t want this, you use an initialization list ( a : after the constructor) to specify the inputs to the constructor. Initialization lists can also initialize variables.

Default constructor = the constructor with no arguments.

Pointers and references

C vs C++

Windows C/C++/C# Flavors

Libraries

A library is a package of code that is meant to be reused by many programs. Typically, a C++ library comes in two pieces:

Libraries are precompiled for several reasons. First, since libraries rarely change, they do not need to be recompiled often. It would be a waste of time to recompile the library every time you wrote a program that used them. Second, because precompiled objects are in machine language, it prevents people from accessing or changing the source code, which is important to businesses or people who don’t want to make their source code available for intellectual property reasons.

There are two types of libraries: static libraries and dynamic libraries.

A static library (also known as an archive) consists of routines that are compiled and linked directly into your program. When you compile a program that uses a static library, all the functionality of the static library becomes part of your executable. On Windows, static libraries typically have a .lib extension, whereas on linux, static libraries typically have an .a (archive) extension. One advantage of static libraries is that you only have to distribute the executable in order for users to run your program. Because the library becomes part of your program, this ensures that the right version of the library is always used with your program. Also, because static libraries become part of your program, you can use them just like functionality you’ve written for your own program. On the downside, because a copy of the library becomes part of every executable that uses it, this can cause a lot of wasted space. Static libraries also can not be upgraded easy — to update the library, the entire executable needs to be replaced.

A dynamic library (also called a shared library) consists of routines that are loaded into your application at run time. When you compile a program that uses a dynamic library, the library does not become part of your executable — it remains as a separate unit. On Windows, dynamic libraries typically have a .dll (dynamic link library) extension, whereas on Linux, dynamic libraries typically have a .so (shared object) extension. One advantage of dynamic libraries is that many programs can share one copy, which saves space. Perhaps a bigger advantage is that the dynamic library can be upgraded to a newer version without replacing all of the executables that use it.

Because dynamic libraries are not linked into your program, programs using dynamic libraries must explicitly load and interface with the dynamic library. This mechanisms can be confusing, and makes interfacing with a dynamic library awkward. To make dynamic libraries easier to use, an import library can be used.

An import library is a library that automates the process of loading and using a dynamic library. On Windows, this is typically done via a small static library (.lib) of the same name as the dynamic library (.dll). The static library is linked into the program at compile time, and then the functionality of the dynamic library can effectively be used as if it were a static library. On Linux, the shared object (.so) file doubles as both a dynamic library and an import library. When using this implicit loading of dynamic libraries, you must a) distribute the .dll with the program and b) tell the linker to link this library.

If you don’t have a .lib file, or want control over when the .dll is loaded, you can use explicit loading. This is done with the LoadLibrary function, which loads the .dll into memory. You also must use the GetProcAddress() to determine the memory location of the function which you intend to use.

Windows, by default, will look for .dll files in the application’s current directory, as well as the path variable. To inspect what is contained within a .dll file on Windows, use the dumpbin utility. To see what dlls are being loaded in Windows, use the process explorer (sysinternals).

Qt

Qobjects

Normally, C++ prefers not using ‘new’ unless necessary. However, Qt uses a hierarchy where a Qobject parent is given in the constructor for a child Qobject. When the parent is deallocated, the children are also deallocated.

“Application has failed to initialize”

Check that required .dll’s have execute permissions.