Zac Gross

Code & More

Notes on C++ for C# Developers

| Comments

After primarily working in C# for the last 5 years I recently switched back to c++ for a large project. I am going to use this article to post notes/gotcha’s/tips as a I come across them. I already have a few to add, and will update as I go along. Hopefully they help some c# programmers out there. They are listed in no particular order.

C++ does not support template type constraints/guards on template definitions

Gotcha: C++ does not support template type constraints/guards on template definitions. I ended up using static asserts (requires c++ 11), the example shown below uses helper methods from the popular boost library.

c#
1
2
3
4
5
class TestSuite<T>
  :  where T : SomeType
{
  
}
C++
1
2
3
4
5
6
template<class Case>
class TestSuite
{

 BOOST_STATIC_ASSERT((boost::is_base_of<ITestCase, Case>::value));
}

By default inheritance is private in c++

C#
1
2
3
4
class b
  : class a
{
}

Don’t forget the “public” declaration if you are used to c#.

C++
1
2
3
4
5
class b
 : public class a
{

}

Member access specified at group level

Access identifier specified on each member.

C#
1
2
public string SomeProp {get; set;}
public string GetSomeOtherProp();

Access can be grouped.

C++
1
2
3
public:
  string GetSomeProp();
  string GetSomeOtherProp();

C++ supports multiple inheiritance

Unlike C#, C++ supports multiple inheiritance, while this may seem like a benifit it should be used very very rarely. If you find your object model requires multiple inheiritance it is best to reevaluate your design or you will end up facing issues like the diamond of death.

Rule of 3

Remember the rule of 3. If you require any of the following then you should explicitly define all three.

  • copy constructor
  • copy assingment operator
  • destructor

Temproraries with Parameterless Constructors

Don’t include parathesis on paramaterless constructors the compiler will favour resolving it as a function declaration and you will end up with an error message like:

1
error: request for member 'method' in 'someObj', which is of non-class type 'someType()'
Parthensis can still be used when using the “new” keyword
1
2
3
4
5
//bad
someObj obj();

//good
someObj obj;

STL containers copy their values.

When adding items to STL containters remember the container does a copy and keeps track of it’s own copy of the item being added.

Follow the Virtual Constructor Idiom

When creating abstract base classes follow the virtual constructor idiom and create virtual clone and create methods. This allows you to create collections of base types while still being able to copy them without knowing their concrete type.

Dynamic Linking Libraries

Here are some tips and tools for working with dynamically linked libraries and shared objects.

  • Shared library files to be registered must start with “lib”
  • Use ldconfig -v to see registered libraries
  • Use ldconfig to reload linked libraries
  • Export ldpath to temporarily point executable at your lib, handy for scripting.
  • Use ldd command to view an executables dependencies.
  • Use nm –demangle to view shared objects exported symbol list.
  • Use c++filt to lookup runtime symbol lookup errors.

Can’t call virtual methods from constructor

Virtual methods can’t be called from within constructors. This usually means you have to use a two stage approach to initialization of derived classes.

Avoid importing namespaces

It is bad practice to use “using namespace <...>;”. When including third party libraries there can be naming conflicts which become a pain to track down in a large project.

Comments