Constant casting

Constant casting is when you are for example calling a external library function that does not understand the const (constant) type and so you will need to take it off because the program will do something funny otherwise, crash. etc..

Here is a example code of a const_cast

#include <iostream>
 
using namespace std;
 
int exampleextenalfuction(int value)
{
	// you can alter the value if you want to, since it is not longer a constant value
	return value  + value;
}
 
int main()
{
	const int constvalue = 2;
 
	cout << constvalue << endl;
 
	cout << "Calling a external function that does not use constant types" << endl;
 
	cout << exampleextenalfuction(const_cast<int&>(constvalue)) << endl;
}

Static casting

Static casting is when you want to convert one type into another, static casting is used allot of the time without even knowing. For example

int value = 2;
float t = float(value);

the float(value) is a static casting of converting one value into another. Below is a full code example

#include <iostream>
 
using namespace std;
int main()
{
	int i = 3;
	// the float(<value)> is basically a static cast from a interger value into a float
	cout << float(i) << endl;
	cout << static_cast<float>(i) << endl;
}

It is very small since static_cast’ing is very basic in nature, but the only problem is that you have to check to make sure that there is a error e.g not NULL.

Execution order

Execution order of programs code is very much a vital thing to understand and where some fault finds will take a long while to figure out. Basically like in maths where there is a order of calculating, well in coding structures and also multitasking and multi-threading setups the execution order may be incorrect for lines of code.

Here is some examples, the first is when will a function be called and the later when post/pre incrementation will take place.

#include <iostream>
 
using namespace std;
 
int value =1;
 
int setvalue2()
{
	cout << "setting value"<<endl;
	value = 2;
	return value;
}
 
int returnvalue()
{
	cout << "renting value"<< endl;
	return value;	
}
 
int main()
{
	// depending on the order of execution the value may be
	/* setvalue2 called first
		(setvalue2 = 2 / returnvalue = 2) = 1
	   returnvalue called first
		(setvalue2 = 2 / returnvalue = 1) = 2
	*/
	cout << setvalue2() / returnvalue() << endl;
 
	int i;
	i = i++ - ++i;	// not sure what i will be because the pre/post increaments 
	i = 3, i++, i++; // i will equal 5 because in correct order.
}

C++ DLL Objects accessed from C#

Because there is a few things that c# cannot do compared to c++, e.g. cpu/memory management. And also there is probably allot of dll’s out there are still required for some events, c# is able to communicate with these files and use there functions within the c# language.

To create an dll within Visual Studio 2005 within the language c++, if you do the following

1. New project -> c++ -> empty project
2. Enter project name (e.g. hellocdll)
3. Right click source files ( in the solution explorer) add-> new item
enter an cpp filename.
4, Right click on the main project heading in the solution explorer -> properties
configuration properties->general inner screen project defaults -> configuration type, alter to dynamic library (.dll)
5. Copy and paste code and then compile.

#include <stdio.h>
 
extern "C"
{
  __declspec(dllexport) void DisplayMessageFromDLL()
  {
              printf ("Hi From the C DLL!\n");
  }
 
  __declspec(dllexport) int DisplayValueAndReturn(int i)
  {
         printf("Value %i\n", i);
         return i+2;
  }
}

The extern “C” means that the references within the code are going to be available externally and marco __declsepc(dllexport) is for the MS compile to inform that functions are to be available within an dll file.

To create an c# project to communicate with the above dll

1. New project -> c# -> empty project
2. Enter project name (e.g. hellodlltest)
3. Right click on the project name ( in the solution explorer) add-> new item
select class and enter a filename
4. Copy and paste the code and then compile. (you may need to change the Dllimport to the dll file name that has been created from the c dll project)

using System;
using System.Runtime.InteropServices;     // DLL support
 
namespace hellodlltest
{
    class hellodllC
    {
        [DllImport("hellocdll.dll")]
        public static extern void DisplayMessageFromDLL();
 
        [DllImport("hellocdll.dll")]
        public static extern int DisplayValueAndReturn(int i);
 
        static void Main ()
           {
                  Console.WriteLine ("C# program 'talking' to an C DLL");
            DisplayMessageFromDLL();
            Console.WriteLine(DisplayValueAndReturn(3).ToString());
           Console.ReadLine()
           }
    }
}

5. Copy the dll file from the debug directory of the c++ created dll project and place into the created bin/debug directory for this c# project

Define – the marco that can save time

The define part of the c/c++ language is able to save on coding time. The define is only really good for a one line of code that a function would be far to much of a over kill.

Here are three examples of the define,

#define MAXVALUE 20

this will allow for a hard coded value to be used within the coding

#define PrintHello cout << "Hello" << endl;

this will print hello when you call the marco as

void main()
{
       PrintHello;
}

and last is passing parameters to the marco

#define PrintWord(word) cout << word << endl;

will change the word parameter within the code to the passed value for example both will work

void main()
{
       PrintWord("hi there");
       PrintWord(3);
}

since the marco will convert the passed parameter to the value.

Operators

An operator syntax is part of the standard C/C++ syntax that allows you to use the +/-/* etc aspects of the objects that are assoicated with them, for example, for two values of an integer you can add these two values together with value1 + value2, the adding part is calculated within the operator and the value is returned.

To code a operator you just need to use the operator () function. Within the code below there are 3 types of operators for the operate test class, to add another integer to the internal value, to increament the internal value and also to logically add the internal value with another integer value.

The code

#include <iostream>
 
using namespace std;
 
class optest
{
public :
       int i ;
       optest() { i = 0; }
       optest(int i2) { i = i2;}
 
       virtual int out() { return i;}
 
       int operator +(int addi)
       {
              return i + addi;
       }
 
       int operator++(int)
       {
              return ++i;
       }
 
       int operator &(int addi)
       {
              return i&addi;
       }
};
 
class optest2 : public optest       // inhert the base class for showing how to use operators within base class and inherited classes
{
public:
       int out() { return i + 10;}
       optest2() : optest() {;}
       optest2(int i) : optest(i) {;}
};
 
int main(int argc, char* argv[])
{
       // setup and demo the results of the operators
       optest op(3);
       optest2 op2(2);
       cout << op.out() << endl; 
       cout << op + 3 << endl;
       cout << op.out() << endl;
       cout << op++ << endl;
       cout << op.out() << endl;
       cout << op2.out() << endl;
       return 0;
}

I have included Microsoft Visual Studio 2005 downloadable edition (here) and also Linux code for this tutorial as a download.

Function Pointers

Function pointers are just like any other type of pointer within C/C++, in that they point to a reference in memory to something that is the reference to the object (a pointer to a integer type that is holding the value 2 is the value of 2 and the pointer just points to this object). Since a pointer can point to any type of object that is similar to the object type, you can also have pointers to functions. This type of pointer is good for having a single function pointer to list of functions that is chosen from a action, for example if there was a list of function for the edit action within the standard document editors (Cut/Copy/Paste/Undo/Redo) and to just point to within every function was required was to use the drop down list item number, then the code is very small and also re-usable.

For example of code with using function pointers.

#include <iostream>
 
using namespace std;
 
void f()
{
       cout << "hi" << endl;       
}
 
void g()
{
       cout << "bye" << endl;
}
 
// always set the function pointer to NULL.
void (*func)() = NULL;
 
// to pass a function pointer to a function.
void callFunc( void (*function)())
{
       function();
}
 
int main(int argc, char* argv[])
{
       // set the func object (as created above – void (*func)() = NULL; - to the function address f)
       func=&f;
       // call the function that is assoicated with the func pointer
       func();
       // reset to another function.
       func=&g;
       func();
 
       // call the function pointed to within another function.
       callFunc(func);
       return 0;
}

I have included Microsoft Visual Studio 2005 downloadable edition (here) and also Linux code for this tutorial as a download.