parameter initialization in constructor

Dec 1, 2011 at 5:19pm
I have a class:

1
2
3
4
5
6
7
8
9
class FlipFlopReg32
{
		int32_t		resetValue;
		int32_t		qCurrent;
		int32_t		qNext;
public:
		FlipFlopReg32(int32_t rv = 0,         // default constructor
			   int32_t c = 0,
			   int32_t n = 0);


What I'd like to do, is if the 2nd and/or 3rd argument is left off the call to the constructor, qCurrent and/or qNext would take the value of resetValue.

Anyone know of a way to code this?

Thanks.
Dec 1, 2011 at 5:23pm
That's not the default constructor as the comment says.

To do what you want, you just need to overload the constructor with a version that only takes rv:

1
2
3
FlipFlopReg32();//Default
FlipFlopReg32(int32_t rv, int32_t c, int32_t n); //Version 1
FlipFlopReg32(int32_t rv); //Version 2 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
FlipFlopReg32::FlipFlopReg32() : resetValue(0), qCurrent(0), qNext(0)
{
    //any other initialization
}

FlipFlopReg32::FlipFlopReg32(int32_t rv, int32_t c, int32_t n) : resetValue(rv), qCurrent(c), qNext(n)
{
    //any other initialization
}

FlipFlopReg32::FlipFlopReg32(int32_t rv) : resetValue(rv), qCurrent(rv), qNext(rv)
{
    //any other initialization
}
Last edited on Dec 1, 2011 at 5:27pm
Dec 1, 2011 at 5:27pm
Oh...very good. Thanks for the quick reply.

Why isn't what I posted the default constructor? Every parameter has a default value...isn't that sufficient criteria?
Dec 1, 2011 at 5:29pm
The default constructor is the one that takes no parameters. While your function can have all its parameters omitted to look like it is the default constructor, your code actually doesn't have a default constructor.
Dec 1, 2011 at 5:32pm
@ L B It actually *is* the default constructor by definition

§12.1[class.ctor]/5
A default constructor for a class X is a constructor of class X that can be called without an argument
Last edited on Dec 1, 2011 at 5:33pm
Dec 1, 2011 at 5:32pm
OK...I thought I remember my teacher saying otherwise, but I'll take your word for it. I think, though, that with a constructor like this, I don't really need a true default c'tor, do I?
Dec 1, 2011 at 5:35pm
No, you don't need a true default constructor - I just included it to show what I meant. The second constructor in my example can have rv = 0 by default.

@Cubbi: When using functions whose arguments have default values, are you sure the values are not implicitly passed? In that case the constructor is receiving arguments that aren't actually visible in the calling code.
Dec 1, 2011 at 5:38pm
@L B Classification of constructors has nothing to do with passing values to functions. It has to do with whether the expression X() is well-formed.
Dec 1, 2011 at 5:41pm
Calling without can argument is different from writing code that does not pass an argument.
Actually, I guess you're right; the this pointer is passed implicitly so even the default constructor is passed an argument...
Last edited on Dec 1, 2011 at 5:41pm
Dec 13, 2011 at 7:05am
I just tried implementing the above solution as follows:

1
2
3
4
		FlipFlopReg32(int32_t rv = 0,         // default constructor
			   int32_t c = 0,
			   int32_t n = 0);
		FlipFlopReg32(int32_t rv);			// single parameter constructor 


And on this line of code, the compiler is telling me that the calls to the *copy* constructor are ambiguous.

Nco::Nco(int32_t rv) : regSin(rv), regCos(rv)

(regSin and regCos are FlipFlopReg32 objects.)

Can someone explain what is going on?
Last edited on Dec 13, 2011 at 7:05am
Dec 13, 2011 at 9:02am
If you call FlipFlopReg32(myInt32_t), you might mean FlipFlopReg32(myInt32_t) or FlipFlopReg32(myInt32_t, 0, 0). The compiler has no way of knowing.
Dec 13, 2011 at 3:33pm
OK, so what's the answer, then. Do I have to remove the defaults from my first constructor?
Topic archived. No new replies allowed.