2

I have a running program in c++ where I am dealing with multiple functions, and I am trying to convert program onto an implementation in which constructors can be used. Program looks like below

bool netlist::create(const evl_wires &wires,
    const evl_components &comps)
{
    return create_nets(wires)&& create_gates(comps);
}

netlist::netlist(const evl_wires &wires,
    const evl_components &comps)
{
    create(wires, comps);
}

where in main function I am calling constructor successfully like this

netlist nl(wires, comps);

Change I am trying to implement looks like this by adding more constructors

netlist::netlist(const evl_components &comps)
{
    create_gates(comps);
}

netlist::netlist(const evl_wires &wires)
{
    create_nets(wires);
}

netlist::netlist(const evl_wires &wires,
    const evl_components &comps)
{
    //this->netlist::netlist(wires);//this approach doesn't work
    //this->netlist::netlist(comps);
    *this = netlist::netlist(wires);//it doesn't work too
    *this = netlist::netlist(comps);
}

Examples given on internet websites including this one just show how to call one constructor inside another, how can we call multiple constructors inside one for my example? Other question is when implementation with constructors is better than implementation without constructors keeping in view of my program code?

Cœur
  • 37,241
  • 25
  • 195
  • 267
aarifboy
  • 41
  • 4
  • 1
    `*this = ` will change the `this` point, so doing it twice can't be what you intend – doctorlove Mar 27 '18 at 19:07
  • 3
    Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of relying on random internet tutorials to learn C++. – NathanOliver Mar 27 '18 at 19:07
  • 1
    Not 100% certain, but it looks like you could be asking about the [Member Initializer List](http://en.cppreference.com/w/cpp/language/initializer_list) – user4581301 Mar 27 '18 at 19:08
  • Tell me what `create_nets` does. Does it change the references you send it, or use it? – doctorlove Mar 27 '18 at 19:09
  • @doctorlove create_nets is using another function in it not shown in code above. Code is like one function calls another, other function calls another one and so on. I would like to change code with constructors at all levels, but right now at first level constructor works, at second level which is where problem is right now I am stuck – aarifboy Mar 27 '18 at 19:26
  • How do you expect people to help you when you don't show all of the relevant code? In any case, what the functions do is irrelevant to THIS question which is about calling constructors from other constructors. If you are having other problems, post new questions about THAT. THIS question has been answered as asked. – Remy Lebeau Mar 28 '18 at 00:53

2 Answers2

4

What you are looking for is probably delegating constructors (introduced in C++11), in which you can call a constructor in another constructor's member initialization list, eg:

netlist::netlist(const evl_components &comps)
{
    create_gates(comps);
}

netlist::netlist(const evl_wires &wires)
{
    create_nets(wires);
}

netlist::netlist(const evl_wires &wires, const evl_components &comps)
    : netlist(wires) // <-- delegating here!
{
    create_gates(comps);
}

Note that you can only delegate one constructor at a time, so in the above example, it is not possible for the 2-param constructor to delegate to both 1-param constructors. But, you could swap around the delegation so the 1-param constructors both delegate to the 2-param constructor:

netlist::netlist(const evl_components &comps)
    : netlist(evl_wires(), comps)
{
}

netlist::netlist(const evl_wires &wires)
    : netlist(wires, evl_components())
{
}

netlist::netlist(const evl_wires &wires, const evl_components &comps)
{
    create_nets(wires);
    create_gates(comps);
}

However, in your particular example, there is little benefit to delegating any of the constructors at all. Since you already have common functions to initialize the two components, just call them directly:

netlist::netlist(const evl_components &comps)
{
    create_gates(comps);
}

netlist::netlist(const evl_wires &wires)
{
    create_nets(wires);
}

netlist::netlist(const evl_wires &wires, const evl_components &comps)
{
    create_nets(wires);
    create_gates(comps);
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • *"error: an initializer for a delegating constructor must appear alone"* – Jarod42 Mar 27 '18 at 19:15
  • I tried this approach but at second netlist after comma I see a red line saying a delegation constructor cannot have other mem-initializers .i.e., netlist(comps). That is why my question says multiple constructors not just a single one – aarifboy Mar 27 '18 at 19:21
  • u just used multiple functions in a constructor which works, but how multiple constructors work in one constructor. question can be is it possible, if yes how? – aarifboy Mar 27 '18 at 19:38
  • @aarifboy: "*but how multiple constructors work in one constructor*" - it is not possible, and I had updated my answer to account for that – Remy Lebeau Mar 27 '18 at 19:52
  • In your first solution if I delegate comps instead of wires and use function create_nets instead of create_gates inside body of contructor I see error map/set iterator not dereferencable during run time not during compile time, why would be that? – aarifboy Mar 27 '18 at 22:20
  • btw in create_gates I am using list comps to iterate and in create_nets I am using a map wires to iterate, is it due to that? – aarifboy Mar 27 '18 at 22:31
  • I can't answer that, since you haven't shown any of that relevant code. – Remy Lebeau Mar 28 '18 at 00:51
0

If you are pre-C++11, you will need to move the common code to separate functions and call them from the constructors.

If you have access to C++11, then use delegating constructors.

Acorn
  • 24,970
  • 5
  • 40
  • 69
  • those work for only one constructor, not two or more. that exactly is my question – aarifboy Mar 27 '18 at 19:22
  • @aarifboy: you need to move the common code, that is, both constructors into one separate function each; then call from each of the 3 constructors the other 2 functions as needed -- that is, if you are using pre-C++11. – Acorn Mar 27 '18 at 19:24