Creating an instance of class

/* 1 */ Foo* foo1 = new Foo (); Creates an object of type Foo in dynamic memory. foo1 points to it. Normally, you wouldn’t use raw pointers in C++, but rather a smart pointer. If Foo was a POD-type, this would perform value-initialization (it doesn’t apply here). /* 2 */ Foo* foo2 = new … Read more

What do constructor type arguments mean when placed *before* the type?

Calling a generic constructor This is unusual alright, but fully valid Java. To understand we need to know that a class may have a generic constructor, for example: public class TypeWithGenericConstructor { public <T> TypeWithGenericConstructor(T arg) { // TODO Auto-generated constructor stub } } I suppose that more often than not when instantiating the class … Read more

How to disable warning on Sonar: Hide Utility Class Constructor?

If this class is only a utility class, you should make the class final and define a private constructor: public final class FilePathHelper { private FilePathHelper() { //not called } } This prevents the default parameter-less constructor from being used elsewhere in your code. Additionally, you can make the class final, so that it can’t … Read more

C++ template constructor

There is no way to explicitly specify the template arguments when calling a constructor template, so they have to be deduced through argument deduction. This is because if you say: Foo<int> f = Foo<int>(); The <int> is the template argument list for the type Foo, not for its constructor. There’s nowhere for the constructor template’s … Read more