1

I got one class named FaultsTablemanager, it has two constructors:

class FaultsTableManager : public QObject
{
    Q_OBJECT
public:
    FaultsTableManager(MainWindow* mainW,
                       QMutex* mu,
                       QWidget* parentPa,
                       QTableWidget* tabA,
                       QRadioButton* sel1,
                       QRadioButton* sel2,
                       QQueue<RDIU*>* que,
                       QTableWidget* tabDe,
                       QWidget* parentDe,
                       QLabel* nameLab,
                       QLabel* timeLab);

    FaultsTableManager(MainWindow* mainW,
                       QMutex* mu,
                       QWidget* parentPage1,
                       QTableWidget* tabA,
                       QRadioButton* sel1,
                       QRadioButton* sel2,
                       QList<RDIU*>* list1,
                       QList<RDIU*>* list2,
                       QTableWidget* tabDe,
                       QWidget* parentDe,
                       QLabel* nameLab,
                       QLabel* timeLab);
...
}

I want to reuse the first constructor in the second one like this:

    FaultsTableManager::FaultsTableManager(MainWindow* mainW,
                                           QMutex* mu,
                                           QWidget* parentPa,
                                           QTableWidget *tabA,
                                           QRadioButton *sel1,
                                           QRadioButton *sel2,
                                           QList<RDIU*> *list1,
                                           QList<RDIU*> *list2,
                                           QTableWidget* tabDe,
                                           QWidget* parentDe,
                                           QLabel* nameLab,
                                           QLabel* timeLab)
    {
        FaultsTableManager(mainW,
                           mu,
                           parentPa,
                           tabA,
                           sel1,
                           sel2,
                           NULL,
                           tabDe,
                           parentDe,
                           nameLab,
                           timeLab);
//        table = tabA;
        queue = NULL;
        list = list1;
        errList = list2;
    }

But if I left the line of code table = tabA; commented, the member table will not be initialized(cannot be used later).
So I think the call of the first constructor will not operate on the object, and only new another object which is not stored in my constructor function.
But I've saw the call of base calss's constructor in subclass's and it operates on the same object which is the reason I use this method.
So is it possible for me to reuse constructor A in constructor B? if not, it seems too redundant and I believe there is a better way to solve this redundance.

Sam Miller
  • 23,808
  • 4
  • 67
  • 87
Al2O3
  • 3,103
  • 4
  • 26
  • 52
  • 6
    are you sure that constructor has enough arguments? perhaps add some? – Cheers and hth. - Alf Mar 10 '13 at 03:06
  • Delegating constructors or default arguments. – Pubby Mar 10 '13 at 03:06
  • 2
    Why not just write a private function called `initialise` and use that in the constructor? – Ed Heal Mar 10 '13 at 03:07
  • @Pubby: or an artificial base class for c++03 – Cheers and hth. - Alf Mar 10 '13 at 03:08
  • @Cheersandhth.-Alf but it has no default arguments, and it is compiled. – Al2O3 Mar 10 '13 at 03:08
  • 2
    @Rubby: consider the [Named Parameter Idiom](http://www.parashift.com/c++-faq/named-parameter-idiom.html). and similar thinking. – Cheers and hth. - Alf Mar 10 '13 at 03:09
  • @EdHeal, so I should add a function which will be used in two constructors to initialize the members? – Al2O3 Mar 10 '13 at 03:10
  • 2
    @Rubby the problem with an `initialize` function is that it can't do actualy initialization, only assignment. – Pubby Mar 10 '13 at 03:12
  • @Rubby - Possibly yes if you need to do stuff other that what can be achieved by initialisation lists. – Ed Heal Mar 10 '13 at 03:13
  • @Pubby, yes, in constructor A I only want to use QQueue member and in constructor B, I only want to use the list member and it is has something to do with different operations on other members like QTableWidget. So I have to use if(list != NULL){}else{} in the constructor A; – Al2O3 Mar 10 '13 at 03:19
  • You can do so if you're using c++11, which allows you to delegate constructors – zzk Mar 10 '13 at 04:02
  • http://stackoverflow.com/questions/308276/c-call-constructor-from-constructor thread has the details for using c++ 11 delegate ctor and the possible options for non c++11 – Viv Mar 10 '13 at 08:29

1 Answers1

2

This is only possible with C++11, and even then, many compilers don't yet have that particular feature implemented. (GCC just recently supports it as of 4.7, but I haven't tested it personally). The feature is called 'Constructor delegation' or 'delegating constructors'.

Visual Studio 2011 and 2012 don't yet support it, as far as I know.

You have to use a Initialize() function (perhaps private) to initialize the common parts of both constructors.

See here for the state of GCC support of C++11 features. (GCC is up to 4.8, and MinGW now is up to 4.7, so the very latest MinGW has the feature as well).

Jamin Grey
  • 10,151
  • 6
  • 39
  • 52