2

Basically, I have a class called VisaMux and a class called MuxPath. MuxPath has a VisaMux private instance variable. I want MuxPath's constructor to assign the instance variable a given VisaMux object without invoking an empty VisaMux() constructor.

5  MuxPath::MuxPath(const uint& Clk_sel, const uint& Lane_sel, const VisaMux& Mux){
6      clk_sel = Clk_sel;
7      lane_sel = Lane_sel;
8      mux = Mux;
9  }

This code results in the error:

MuxPath.cpp:5: error: no matching function for call to ‘VisaMux::VisaMux()’
VisaMux.h:20: candidates are: VisaMux::VisaMux(const std::string&, const uint&, const uint&, const std::vector<VisaLane, std::allocator<VisaLane> >&, const std::vector<VisaResource, std::allocator<VisaResource> >&)

As you can see, it errors on the first line (line 5), so it seems that somehow const VisaMux& Mux is invoking VisaMux(), which doesn't exist. This also happens if I just do VisaMux Mux.

I don't want it to call an empty constructor for VisaMux because I want VisaMux to be created only by passing its constructor all the necessary parameters.

How can I do this?

Drew Wiens
  • 124
  • 1
  • 8
  • The answers are below and tell you to use the initialization list syntax. I just want to add that you should prefer the use of the initialization syntax, otherwise you often construct objects in your parameter list twice. Once by default and once inside the body of your constructor. Also you should put items in your initialization list in the same order you have them declared (see S. Meyers Effective C++ item 13) – Tod Jul 20 '11 at 18:39
  • @Andrew Wiens: Have a look at this Answer [here](http://stackoverflow.com/questions/6724626/c-constructor/6724639#6724639) explains you the difference between memberwise initialization and assignment inside constructor body – Alok Save Jul 20 '11 at 18:43

5 Answers5

6

Use the constructor initialization list:

MuxPath::MuxPath(const uint& Clk_sel, const uint& Lane_sel, const VisaMux& Mux)
       : clk_sel(Clk_sel)
       , lane_sel(Lane_sel)
       , mux(Mux)
{}
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
5

Use member-initialization-list in the constructor as:

MuxPath::MuxPath(const uint& Clk_sel, const uint& Lane_sel, const VisaMux& Mux) 
   :clk_sel (Clk_sel),lane_sel(Lane_sel),mux(Mux)
{ //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it's called initialization-list

}

Actually, in your code, all the member variables use assignment rather than their respective constructor, which means mux tries to get constructed with default constructor, even before it enters into the constructor of MuxPath. And sinceVisaMux doesn't have default constructor, its giving compilation error.

So by using the initialization-list, in which the syntax mux(Mux) invokes the copy-constructor of VisaMux, you avoid the invocation of default constructor of VisaMux which doesn't exist. And since the mux is already copy-constructed, there is no need to use assignment in the constructor body.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
0
   MuxPath::MuxPath(const uint& Clk_sel, const uint& Lane_sel, const VisaMux& Mux)
       : mux(Mux)
   {
       clk_sel = Clk_sel;
       lane_sel = Lane_sel;
   }

It's called "initialization list".

littleadv
  • 20,100
  • 2
  • 36
  • 50
0
class MuxPath {
  MuxPath(const uint& Clk_sel, const uint& Lane_sel, const VisaMux& Mux)
    : clk_sel(Clk_sel), lane_sel(Lane_sel), mux(Mux) {};
  ...
};
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
0

You're kind of asking how you can get red walls in your house without first installing wall studs. If your MuxPath class contains a Mux variable, at some point during its construction, it's going to need to instantiate a variable of type Mux. This implies that an instance of type Mux will be created, and the only mechanism to do that is with a constructor call.

This can either be a default, or no-arg constructor, a copy constructor, or a constructor that takes in some other argument. The other answers show how to do that in the member initialization list. But there's no way to get around the fact that at some point, some contructor for Mux will need to be called.

JohnMcG
  • 8,709
  • 6
  • 42
  • 49