944,029 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 705
  • C++ RSS
Nov 9th, 2009
0

Apparent flaws in C++

Expand Post »
[code]
class A {
A& a;
public:
A(): a(* new A()) {}
};
[code]

There is a huge flaw here and before I compiled this code I was hoping that it will fail to compile, But it didn't. At first I felt like I had a big flaw in C++ as it allowed data member pointers of the same kind in a class, but then I realized the flaw was in that it is a recursive function call, which will form an infinite loop. Apart from recursive functions, which other methods do you ppl know about C++?
Similar Threads
Reputation Points: 37
Solved Threads: 3
Newbie Poster
tintin.iitk is offline Offline
22 posts
since Nov 2009
Nov 9th, 2009
-7
Re: Apparent flaws in C++
C is designed with the assumption that if the the programmer does something, however strange it seems, it is because he means to do it.
Moderator
Featured Poster
Reputation Points: 1800
Solved Threads: 575
Moderator
jbennet is offline Offline
16,525 posts
since Apr 2005
Nov 9th, 2009
0
Re: Apparent flaws in C++
That's a good way of looking at it.. but anyway I was more interested in knowing what other such necessary-to-avoid situations are.. like dynamic memory allocation is another such thing. Well, I'm a beginner as far as C++ programming is concerned(mostly used java) and was curious to know what all things I should be cautious about. Anyway, Java did solve the problem of leaked dynamic memory allocation through its internal junk management but C/C++ are not much safe (ok, smart pointers and std::auto_ptr are alternatives, but still not foolproof)about that. Returning to the original problem, even Java suffers from this risk of falling into infinite loops dues to recursive function calls.
Reputation Points: 37
Solved Threads: 3
Newbie Poster
tintin.iitk is offline Offline
22 posts
since Nov 2009
Nov 9th, 2009
2
Re: Apparent flaws in C++
Returning to the original problem, even Java suffers from this risk of falling into infinite loops dues to recursive function calls.
Click to Expand / Collapse  Quote originally posted by jbennet ...
C is designed with the assumption that if the the programmer does something, however strange it seems, it is because he means to do it.
C is notorious for giving you "enough rope to shoot yourself in the foot"--but it doesn't matter what language you're talking about... if it allows you to define a recursive function, you can always define one that never terminates.

Allowing recursion without this possibility is sort of like inventing a language in which untruthful statements aren't grammatical--a warm, fuzzy thought, but such a thing simply doesn't exist.

Sometimes recursion loops are detectable, but no development environment I've ever worked with has even bothered to try.

The halting problem is at the heart of this issue... I recommend you study it if you haven't seen it already. This version is more entertaining.
Reputation Points: 182
Solved Threads: 72
Posting Pro in Training
gusano79 is offline Offline
476 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: arithmetic calculator
Next Thread in C++ Forum Timeline: c or c++ program to excute at a specified time





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC