Reference to pointer in C++

Reference to pointer is a useful construct to be aware of in C++. As you might already know, the C++ language allows you to take a reference to any object that is not a temporary. You can take a const reference to any object, including a temporary. So what is special about reference to a pointer?

A common construct in C++ is to receive a pointer to a pointer as input argument to a function. This is typically used to allocate a primitive or an object inside the function. The allocated primitive or object will be available in the caller after the function is called and done with. This idiom is common with C programmers who have moved to C++. It is such code that becomes a lot cleaner and easier to write and read if a reference to pointer is used as input argument to function. As a bonus, since it is a reference it has to always refer to a pointer that actually exists.

The code example below shows the difference between pointer to pointer and reference to pointer:

Note how reference-to-pointer is cleaner to understand both at the caller location and inside callee.

Advertisements

How to add a .Net assembly as reference in IronPython

Problem

By installing IronPython and Python Tools for Visual Studio, I can work with .Net using Python. The typical method of adding a .Net assembly file as a Reference is to use choose Add Reference in Visual Studio. But, doing this did not work for importing a .Net assembly built using C#. The assembly appeared in the References list, but had a Warning sign over it. And sure enough, importing a namespace that the assembly exports failed with a ImportException.

Solution

Apparently, the Add Reference method only works for Python Extension Modules, not for .Net assemblies written in C#. Instead use the methods in clr module to add the reference:

1. Place the .Net assembly file, say Foo.dll, and any other DLL files that it is dependent on in the directory where your IronPython source file lies.

2. Add a reference to this .Net assembly file in your IronPython using and import the namespace exported by the .Net assembly in your IronPython code.

Note: Remember to place all the DLL files that the .Net assembly is dependent on along with it in the same directory. Else you will get a FileNotFoundException.

Tried with: IronPython 2.7.3, Visual Studio 2012 and Windows 7 x64