Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling overloaded constructor from constructor initialisation list
    primarykey
    data
    text
    <p>In the code below, my intent is to call one of two overloaded constructors for the <code>kap</code> (class <code>opacity</code>) based on what arguments are passed to the object of class <code>material</code>:</p> <pre><code>class opacity{ private: int mode; double kap_const; double kappa_array[10][10]; public: opacity(double constkap); // picking the constructor sets the mode opacity(char* Datafile); double value(double T, double P); // will return a constant or interpolate }; opacity::opacity(double constkap):mode(1){ kap_const = constkap; } opacity::opacity(char* Datafile):mode(2){ // read file into kappa_array... } class Matter { public: Matter(int i, double k, char* filename); // many more values are actually passed opacity kap; int x; // dummy thing // more variables, call some functions }; Matter::Matter(int i, double k, char * filename) :x(k&gt;0? this-&gt;kap(x): this-&gt;kap(filename) ) { // ... rest of initialisation } </code></pre> <p>This is however not working:</p> <pre><code>test.cpp: In constructor 'Matter::Matter(int, double, char*)': test.cpp:32:21: error: no match for call to '(opacity) (void*&amp;)' test.cpp:32:42: error: no match for call to '(opacity) (char*&amp;)' test.cpp:32:44: error: no matching function for call to 'opacity::opacity()' test.cpp:32:44: note: candidates are: test.cpp:20:1: note: opacity::opacity(char*) test.cpp:20:1: note: candidate expects 1 argument, 0 provided test.cpp:16:1: note: opacity::opacity(double) test.cpp:16:1: note: candidate expects 1 argument, 0 provided test.cpp:4:7: note: opacity::opacity(const opacity&amp;) test.cpp:4:7: note: candidate expects 1 argument, 0 provided </code></pre> <p>The first thing I had tried,</p> <pre><code>Matter::Matter(int i, double k, char * filename) :kap(k&gt;0? k: filename) { // use k&lt;0 as a flag to read from filename // ... rest of initialisation } </code></pre> <p>also failed, because "the result of a ternary operator always has to be the same type" for compile-time reasons, as pointed out in a <a href="https://stackoverflow.com/questions/4693629/ternary-operator?answertab=votes#tab-top">similar question</a> (although they were not explained there, it seems).</p> <p>Now, the inelegant solution would be to also overload the <code>Matter</code> constructor based on the arguments that the <code>kap</code> constructor should receive, but this is (1) very inelegant, especially since the <code>Matter</code> constructor takes many variables and performs many actions (so a lot of code would be duplicated just to vary the <code>kap</code> part of the constructor initialisation list), and (2) this can get out of hand if there is another class used with <code>Matter</code> that also has different constructors: for <em>M</em> classes with <em>N</em> c'tors, one ends up with <em>N</em>^ <em>M</em> combinations...</p> <p>Would someone have a suggestion or a work-around? Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload