1

I try to delegate a class constructor in Qt 5.4.0 using C++. The problematic code looks as follows:

//.h
class A : public QObject
{

public:
  A(QObject *parent = 0) : A(10, parent) {};   <--COMPILER ERROR
  A(int x, QObject *parent = 0);
  ...
};

//.cpp
A::A(int x, QObject *parent) : QObject(parent)
{
  do_something();
}

The compiler claims that the element initialization 'A' is neither Basis nor Element. Isn't this supported? If so, what causes this error?

quant_dev
  • 6,181
  • 1
  • 34
  • 57
user3482407
  • 319
  • 3
  • 18

1 Answers1

2

Constructor delegation is a C++11 feature that is not supported by all compilers yet. Chances are that it does not work if you are using a Visual Studio compiler (or an old GCC).

See for example this question, with answers stating that it is not even fully supported in VS2012: Is there a way to use delegating constructors in Visual Studio 2012?

For reference, here you can find all features that are supported by the respective MSVC versions (delegating Constructors is mentioned as not supported on MSVC11): https://msdn.microsoft.com/en-us/library/hh567368.aspx

Community
  • 1
  • 1
anderas
  • 5,744
  • 30
  • 49