Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

Mixing default and non-default parameters in Constructor?


Are there some way to mix default and non-default parameters in a
constructor:

class test {

public:
        test(int i = 45, int j) : pp(j){}
private:
        int pp;
        int i;

};

The above does not work and gives the error:

main.cpp:144: error: default argument missing for parameter 2 of
test::test(int, int)
make: *** [main.o] Error 1

Yes, but default arguments have to be the last one[s].

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

desktop wrote:
> Are there some way to mix default and non-default parameters in a
> constructor:

> class test {

> public:
>     test(int i = 45, int j) : pp(j){}

       test(int j, int i_ = 45) : pp(j), i(i_) { }

> private:
>     int pp;
>     int i;
> };

> The above does not work and gives the error:

> main.cpp:144: error: default argument missing for parameter 2 of
> test::test(int, int)
> make: *** [main.o] Error 1

Default parameters must be at the *end* of the parameter list.
Note also, that you did not initialize your member variable "i".
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc