Talk:Criticism of C++/Archive 1
This is an archive of past discussions about Criticism of C++. Do not edit the contents of this page. If you wish to start a new discussion or revive an old one, please do so on the current talk page. |
Archive 1 |
list of problems with c++
Here's some problems with C++ that I have encountered. There's a few things that the language doesn't let you do, for no obvious reason. There's also a few things which it lets you do, that really it shouldn't, because they promote bad code and have no great advantage over doing things a different way. Finally, there are some things that just plain work in a weird way, which form a minefield for anyone attempting to use them who lack the experience to steer clear.
Missing features
- initialising non-integer static const variables inside a class header
- recover the size of an array at run time
Features that don't work (or don't work as you'd expect)
- you are allowed to override operators (&&), (||) and (,) but it is a really bad idea
- you can create your own type conversion operator()'s, but it is not good practice, as unintended results are likely
- you can call virtual functions from constructors or destructors, but they do not work as you would expect.
- the order of initialisation of global objects is undefined, which may cause errors if there are dependencies between them
- exception specifications generally aren't implemented, and in any case do not work the way you'd want them to
- redefining the default parameter values for inherited virtual functions doesn't work properly (they ain't virtual)
- single parameter constructors are used as implicit type conversion operators by default, which seems wrong
- you can use arrays of objects polymorphicly, but it doesn't work (deletion and indexing can both fail).
The use of templates to achieve certain affects could be seen as a hack to make up for some of the inadequacies of the c++ preprocessor.
Low article quality
This article is not well recherched and requires serious rework before merged. 85.88.24.15 (talk)
The topic "Criticism of C++" is a requested article on wiki. Hence an effort has to be made to start somewhere so that it can be revamped by experts once the work gets started. Aandu (talk) 08:38, 26 December 2007 (UTC)
The following Topics need to be elaborated upon:
No Dynamic type safety Criticism of features that break Object Orientation Standardization drive Maintenance Economy of expression Tools for mitigating issues with C.... Aandu (talk) 08:46, 26 December 2007 (UTC)
Let me add a topic for elaboration: In my experience, compared to C, C++ programs tend to end up vastly more bloated, overly-complex, and hard to follow. This seems to be from a proclivity towards major over-and-mis-use of Object Oriented programming as a universal panacea, where every stupid little thing becomes an elaborate class. Discuss. —Preceding unsigned comment added by 24.18.201.182 (talk) 11:43, 29 December 2007 (UTC)
While the article is not of high quality, I can certify there is no original research on it, thus I'm removing the warning about this. I'm an experienced C++ user, and I am expert enough to certify this (because the content of the article is commonly known among C++ users).--Blaisorblade (talk) 01:43, 20 June 2008 (UTC)
Btw, C++#Criticism seems to have better style than this article.--Blaisorblade (talk) 02:04, 20 June 2008 (UTC)
syntax for range
There is really syntax for range.
example:
#include<cstdio>
unsigned int n;
int main(){
scanf("%u",&n);
switch(n){
case 0:
puts("This is zero");
break;
case 1 ... 9:
puts("It has one digit");
break;
case 10 ... 99:
puts("It has two digits");
break;
default:
puts("It has more than three digits");
}
return 0;
}
The above is not valid C++. I think the ranges in a case statement (1...99) is an extension of the GNU/GCC compiler. 195.14.207.109 (talk) 15:33, 3 June 2008 (UTC)
- I confirm this, see [1]. --Blaisorblade (talk) 01:51, 20 June 2008 (UTC)
function as parameter
This is an example using a function as a parameter
#include<cstdio>
void swap(int&a,int&b){
int c=a;
a=b;
b=c;
}
void sort(int*arr,int n,bool cmp(int,int)){
for(int i=0;i<n-1;i++)
for(int j=i;j>=0;j--)
if(cmp(arr[j+1],arr[j]))
swap(arr[j],arr[j+1]);
}
bool reverse(int a,int b){
return a>b;
}
int main(){
int list[]={1,2,3,4,5};
sort(list,5,reverse);
for(int i=0;i<5;i++)
printf("%d\n",list[i]);
return 0;
}
The 3rd parameter of
sort(int*,int,bool(int,int))
is really a function. —Preceding unsigned comment added by Michaeldadmum (talk • contribs) 13:00, 12 January 2008 (UTC)
In the above sort function, "bool cmp(int,int)" is invalid. It has to be a function pointer. 195.14.207.109 (talk) 15:33, 3 June 2008 (UTC)
- The correct way to define a function parameter, in accordance to C How To Program Second Edition by H. M. Deitel and P. J. Deitel, is:
<TYPE> (*<NAME>)(<PARAMS>)
- So, the correct way to define a function as a parameter in the above function sort is:
void sort(int *arr, int n, int (*cmp)(int &, int &))
- This is explicitly mentioned in the last section of chapter 7: "Pointers". Graham (talk, contrib, SIGN HERE!!!) 16:10, 20 July 2008 (UTC)