View Full Version : Constructors with multiple arguments?
endasil
01-04-2004, 06:16 AM
if i have a class with a constructor taking two arguments, how do i set the values for it?
With a constructor taking one argument, i can do it like this:
class TestClass
{
public:
TestClass(int value) { a = value}
private:
int a;
};
TestClass Test[5] = {1,4,7,4,9}
With a class taking two arguments, the same does not work, how do i send it values at initialization when the constructor takes
multiple arguments? Like this one:
class B
{
B(int W, int H) { Width = W; Height = H }
private:
int Width, Height;
};
svero
01-04-2004, 07:30 AM
Not sure about the array rules and whether you can only do it with a simple type but you could possibly just define a 2nd struct or class to encapsulate the multiple arguments.
struct Size
{
int width;
int height;
};
class B
{
private :
Size m_size;
public :
B(size) { m_size=size; }
};
B someBees[5] = {
B(100, 100),
B(200, 200),
B(50, 50),
B(25, 75),
B(300, 10)
};
If your class does anything funky with pointers, make sure you implement a good copy constructor. If not, the default copy ctor should suffice.
Fenix Down
01-04-2004, 05:19 PM
That's a pretty nifty way to do that, gfm. You should always implement a copy constructor if you have pointer members in the class, but how is that related to this?
svero
01-04-2004, 06:21 PM
Originally posted by Fenix Down
That's a pretty nifty way to do that, gfm. You should always implement a copy constructor if you have pointer members in the class, but how is that related to this?
Default copy constructors are good for simple types like a bunch of ints etc... but if you have pointers and memory pointed to as part of the class, you'd have to do the copy yourself to create the other class. A simple default copy contructor won't work.
That is... B b = B(10,20); might not be a simple copy. In fact constructing a b might not be a good route depending on how trivially the B class is created. For that reason you might want to consider creating a size struct or a 2nd class like I originally suggested. I think
B someBees[5] = { size(1,2), size(2,3) }
should also work... but you may need to set up some default constructors in either case.
Some (old, not so great) compilers will create a temporary object for each B(W, H) and then copy it to the associated array slot. Most newer and less braindead compilers will be smarter about it and just create the objects in place once.
I haven't checked the C++ Standard to see what it says about this and if one case is 'correct' or if either is allowed, but I've seen both occur in practice, so better to be safe than sorry and make sure you have a good copy constructor if you need one. Of course (as you said) you should implement a copy constructor anyway when dealing with pointer members.
For that reason you might want to consider creating a size struct or a 2nd class like I originally suggested.
Also, FWIW, in the specific case given I also think it makes more sense to use a simple helper class (for things like Point, Size, etc). I just wanted to let the original poster know you can do multiple-argument array initializations if you really want to.
svero
01-04-2004, 06:40 PM
Originally posted by gfm
Also, FWIW, in the specific case given I also think it makes more sense to use a simple helper class (for things like Point, Size, etc). I just wanted to let the original poster know you can do multiple-argument array initializations if you really want to.
Yeah. It really depends on the specific scenario. In some cases it's more elegant to create the helper class and in others it's just mucking up the waters a bit. If it is something like a size class with width and height I'd go with a helper class just because very often there ends up being some things you want to do like compute the area, or the difference between to sizes and so on... so those funcs fold nicely into the helper.