Default arguments and constructors

A

Alfonso Morra

If one wants to pass default arguments to a constructor, what is the
best way to do it, (other than overloading the constructor and writing a
helper function)?

MyClass MyClass(int, int);
MyClass MyClass(int,int,float) ;
void helper_func(int,int, float = PI) ;

I mean is there anyway one can write something like this :

MyClass c* = new MyClass(int arg1, int arg2, float arg3 = PI) ?


(AFAIK, this is illegal, as the compiler dosen't grok it.

Tks
 
K

Karl Heinz Buchegger

Alfonso said:
If one wants to pass default arguments to a constructor, what is the
best way to do it, (other than overloading the constructor and writing a
helper function)?

MyClass MyClass(int, int);
MyClass MyClass(int,int,float) ;
void helper_func(int,int, float = PI) ;

I mean is there anyway one can write something like this :

MyClass c* = new MyClass(int arg1, int arg2, float arg3 = PI) ?

Since when are default arguments specified at the place where
a function is invoked ? :)
(AFAIK, this is illegal, as the compiler dosen't grok it.

Have you tried it?
It is perfectly legal and any compiler should accept it.

class MyClass
{
MyClass( int, int, float = PI );
};
 
S

Srini

Alfonso said:
If one wants to pass default arguments to a constructor, what is the
best way to do it, (other than overloading the constructor and writing a
helper function)?

MyClass MyClass(int, int);
MyClass MyClass(int,int,float) ;
void helper_func(int,int, float = PI) ;

Instead, you can do this in the ctor itself,

MyClass(int , int, float = PI);

Why have you repeated the class name twice?? Typo??
I mean is there anyway one can write something like this :

MyClass c* = new MyClass(int arg1, int arg2, float arg3 = PI) ?


(AFAIK, this is illegal, as the compiler dosen't grok it.

Tks

It is illegal -

1) You cannot specify types while passing arguments to a constructor
2) You have to pass values to the constructor for constructing an
object

MyClass *c = new MyClass(10, 20);
MyClass *d = new MyClass(10, 20, 30.0);

Regards,
Srini
 
P

Philippe Amarenco

Alfonso Morra said:
If one wants to pass default arguments to a constructor, what is the
best way to do it, (other than overloading the constructor and writing
a helper function)?

MyClass MyClass(int, int);
MyClass MyClass(int,int,float) ;
void helper_func(int,int, float = PI) ;

I mean is there anyway one can write something like this :

MyClass c* = new MyClass(int arg1, int arg2, float arg3 = PI) ?


(AFAIK, this is illegal, as the compiler dosen't grok it.

what about this:

class MyClass
{
MyClass(int a, int b, float c = PI) { ... }
};

MyClass *x = new MyClass(42, 0);



or did I miss your question?
 
S

Sharad Kala

Alfonso Morra said:
If one wants to pass default arguments to a constructor, what is the
best way to do it, (other than overloading the constructor and writing a
helper function)?

MyClass MyClass(int, int);
MyClass MyClass(int,int,float) ;
void helper_func(int,int, float = PI) ;

struct MyClass {

MyClass(int i, int j, float k = 3.14){
}
};

int main()
{
MyClass m1(1, 2);
MyClass m2(1, 2, 6.28);
}

HTH,
Sharad
 
A

Alfonso Morra

Srini said:
Instead, you can do this in the ctor itself,

MyClass(int , int, float = PI);

Why have you repeated the class name twice?? Typo??

Yes. Sorry, I left out the scope resolution operator in my haste. My bad.
It is illegal -

1) You cannot specify types while passing arguments to a constructor
2) You have to pass values to the constructor for constructing an
object

MyClass *c = new MyClass(10, 20);
MyClass *d = new MyClass(10, 20, 30.0);

Regards,
Srini
Thats what I thought - thanks for the clarification. I suppose I could
still use overloaded constructors and a helper function though to make
the library easier to use.
 
A

Alfonso Morra

Philippe said:
what about this:

class MyClass
{
MyClass(int a, int b, float c = PI) { ... }
};

MyClass *x = new MyClass(42, 0);



or did I miss your question?

Phil, did you try to compile this?.
 
K

Karl Heinz Buchegger

Alfonso said:
Phil, did you try to compile this?.

Of course he did not as this is incomplete code (as was your posted
example).

Instead of letting us guess, why don't you tell us what your
compilers error message is. I bet it has something to do
with the way you defined 'PI'.
 
A

Alfonso Morra

Karl said:
Of course he did not as this is incomplete code (as was your posted
example).

Instead of letting us guess, why don't you tell us what your
compilers error message is. I bet it has something to do
with the way you defined 'PI'.
This issue has been resolved. See my response to Srini's post.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top