-
April 11th, 2006, 08:12 AM
#1
defalut arguments
why default arguments must be trialing arguments?
why can't be as leading or middle arguments in argument list?
-
April 11th, 2006, 08:16 AM
#2
Re: defalut arguments
?? what are you trieing to say
-
April 11th, 2006, 08:37 AM
#3
Re: defalut arguments
To avoid problems with ambiguity. Suppose you have function of 5 integer arguments and second and forth are optional. In the program this function is caleed with 4 parameters. How can compiler find out which parameter has to have the default value
-
April 11th, 2006, 08:39 AM
#4
Re: defalut arguments
after the second all parameters must be have a default variable. on my compiler atleast
-
April 11th, 2006, 10:07 AM
#5
Re: defalut arguments
Originally Posted by Mitsukai
after the second all parameters must be have a default variable. on my compiler atleast
On all compilers. I believe it might even be a part of the standard.
As Dragforce said, this is to avoid ambiguity.
Code:
void someFunc(int a, int b, int c = 4, int d = 5, int e);
...
someFunc(10, 11, 13, 14);
In the call to 'someFunc' what paramaters get what values? Which one is the parameter that I wanted to have the default value?
VIggy
-
April 11th, 2006, 12:06 PM
#6
Re: defalut arguments
It is, indeed, part of the standard. Personally, I'd like the standard to allow something like
Code:
someFunc(10, 11, , 13, 14);
that is, args are matched strictly in order, with a blank argument meaning "accept the default". In this case, Viggy's example would be an error, because there's no match for "e".
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
April 11th, 2006, 12:50 PM
#7
Re: defalut arguments
Originally Posted by Graham
It is, indeed, part of the standard. Personally, I'd like the standard to allow something like
Yes, IMHO, it would not complicate much the language, and could be a real benefit.
An, alternative would be:
Code:
someFunc(10,11,default,13,14);
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
April 11th, 2006, 12:57 PM
#8
Re: defalut arguments
C++ stndards shuld also allow static class operator and a parent class operator.
-
April 11th, 2006, 01:36 PM
#9
Re: defalut arguments
Originally Posted by Mitsukai
C++ stndards shuld also allow static class operator and a parent class operator.
I don't see any practical application of static member operators.
The this parameter is an implicit parameter of the operator.
Would you want to have an operator whose one parameter (maybe the only parameter) is missing?
Except, in some very particular cases, for operator () in functors.
And, in that case, a non-static operator () does not harm!
parent class operator?
I don't see what you mean.
Perhaps the fact that operators are inherited?
But, actually operators are already inherited!
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
April 11th, 2006, 01:42 PM
#10
Re: defalut arguments
in my OOP programming OpenGL i had a need for static operators! just that its not logical doesntmean its not needed, thats why C++ got big in first place, the flexibility and the usefullness, so why limit operators to non-statics.
and with parent i mean this:
class A{ int c; class B{ B(void){ parent->c = 123 } }; };
-
April 11th, 2006, 02:48 PM
#11
Re: defalut arguments
For this to make sense enclosed class must actually have parent object, which it doesn't. You can provide it directly simply enough.
"Programs must be written for people to read, and only incidentally for machines to execute."
-
April 11th, 2006, 03:39 PM
#12
Re: defalut arguments
Originally Posted by Mitsukai
in my OOP programming OpenGL i had a need for static operators
Please share the code!
Originally Posted by Mitsukai
class A{ int c; class B{ B(void){ parent->c = 123 } }; };
You misunderstood the concept of nested class!
There is no one-to-one correspondance between instances of A and B!
A and B have independent instances.
Declaring B inside A, have effect only on the scope of B.
In order to have this one-to-one correspondance, you can simply pass the this pointer of A in a constructor of B:
Code:
class A
{
int c; // note that c must precede b in this class definition, otherwise, behaviour is undefined!
B b;
class B{ B(A* parent){ parent->c = 123 } };
A():b(this) {}
};
I already had something similar.
IMHO, it is perfectly clean, and does not require any language change!
I experienced similar problems that were perfectly resolved by private inheritance (knowing that C++ allows multiple inheritance).
It is useless to bloat the language with some seldomly used features that are perfectly emulated without language change.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
April 11th, 2006, 03:44 PM
#13
Re: defalut arguments
Originally Posted by SuperKoko
It is useless to bloat the language with some seldomly used features that are perfectly emulated without language change.
It's OK for C++.
"Programs must be written for people to read, and only incidentally for machines to execute."
-
April 11th, 2006, 03:44 PM
#14
Re: defalut arguments
Originally Posted by SuperKoko
Please share the code!
You misunderstood the concept of nested class!
There is no one-to-one correspondance between instances of A and B!
A and B have independent instances.
Declaring B inside A, have effect only on the scope of B.
In order to have this one-to-one correspondance, you can simply pass the this pointer of A in a constructor of B:
Code:
class A
{
int c; // note that c must precede b in this class definition, otherwise, behaviour is undefined!
B b;
class B{ B(A* parent){ parent->c = 123 } };
A():b(this) {}
};
I already had something similar.
IMHO, it is perfectly clean, and does not require any language change!
I experienced similar problems that were perfectly resolved by private inheritance (knowing that C++ allows multiple inheritance).
It is useless to bloat the language with some seldomly used features that are perfectly emulated without language change.
i know about that, i sayd C++ shuld have a parent operator without having the need of the constructor like you just did
-
April 11th, 2006, 04:04 PM
#15
Re: defalut arguments
Originally Posted by RoboTact
It's OK for C++.
C++ has a few useless features inherited from C, but otherwise, has a full set of general purpose features that allow anybody to write very high-level (i.e. very abstract) code.
From that, C++ is extended by librairies!
No need for builtin smart pointers : A library can emulated it!
No need for builtin dynamic array : there is std::vector!
etc...
With templates, you can really do many things that are builtin in other languages.
Originally Posted by Mitsukai
i know about that, i sayd C++ shuld have a parent operator without having the need of the constructor like you just did
The problem, is that this thing would be only used by very few programmers.
This feature does not increase the abstraction level, nor increas maintainability. That is, the workaround does loose nothing.
It is a bit more verbose, but that is not a problem. It will only increase a bit your program (except if your program is full of design flaws), at the cost of your fingers.
Hundreds other minor features avoiding manual writing in very specific cases, are proposed on comp.std.c++.
But there are far too many. That would increase the language specification by a factor ten!
C++ must be learnable and implementable and extensible!
Nowadays, C++ is already very extensible (the boost library is a proof).
The future C++ standard will focus more on librairies than the core language.
If you want to preserve your fingers from typing, you'll gain much more, using these horrible macos:
Code:
#define cl class
#define fr for
#define wl while
typedef char ch;
typedef int it;
typedef double dl;
// ...
Last edited by SuperKoko; April 11th, 2006 at 04:09 PM.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|