Talk:C++/Archive 9

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
Archive 5 Archive 7 Archive 8 Archive 9 Archive 10 Archive 11 Archive 12

dialects in C++ infobox

I think they should go. These are not really dialects but revisions of one language (ie one is invalidated by another), but im not really sure on dialect definition to axe it on my own. Any insight? I compared with C and we dont have any dialects there (even though dialects section would be atually applicable there imo) —Preceding unsigned comment added by 83.25.95.6 (talk) 19:06, 14 August 2008 (UTC)

Better Hello World program interface

I propose the following:

 // Hello World program. 
#include <iostream>
using namespace std;

int main()
{
   cout << "Hello, world!";
   
   return 0;
 
}

The above is much simpler than the current format, and also displays several more functions, including the return line and the std namespace declaration. —Anonymous DissidentTalk 11:57, 4 September 2008 (UTC)

While I'm not opposed to changing the "hello world" example (assuming it has consensus), I do feel strongly that there should not be more than one example in the article; hence my reversion of the addition of the above code. SHEFFIELDSTEELTALK 13:42, 4 September 2008 (UTC)
Sidhantx just changed the hello world example to using namespace std; and endl rather than "\n". Was there a definitive vote as to how this should appear? --Autopilot (talk) 23:03, 11 January 2009 (UTC)

Hoping to satisfy all parties, and also very much in favour of a hello world program as an opportunity to show some representative syntax, I propose this, which is almost the same as the above suggestion:

#include <iostream>

int main()
{
   std::cout << "Hello, world!";
   return 0; //not strictly necessary in the main() function
}

While I don't think a comment like this is very pretty, it is much less displasing and annoying than the confusion arising from visitors' broken expectations and the frustration of the reversion cadre being forced to police this page. Yes, it might be that return is elided in the main() function, but the hello world should be a taste at a glance, not play off technical subtleties. The source for the example is "The C++ Language 3rd Edition"; this is not a beginners book and none such I've perused have omitted an explicit (though strictly speaking semi-redundant) return statement. When presenting a topic, regardless whether from a pedagogical or rethorical perspective, you never start with the minute or technical irregularities, you provide an overview and establish familiarity. Encyclopaedias are no different, if anything, due to their succinctness they must even more so rely on this principle. Miqademus (talk) 18:21, 20 October 2008 (UTC)

Update: Btw, the DJGPP article features this screenshot of a Hello, world! program. 130.243.247.165 (talk) 22:20, 23 October 2008 (UTC)

Over a week and no objections. I take that as consensus and will update the "Hello, World!" sample accordingly (I will omit the comment about return being superfluous). Please discuss here before reverting. Miqademus (talk) 02:02, 31 October 2008 (UTC)

Fundamentally, I have no objection to this version; however, the reason we went with the exact text from Stroustrup was because of the endless edit wars over the exact syntax of the HW program - copying directly from Stroustrup was an attempt to compromise on a single version and avoid every editor coming along and adding his own preferred syntax. Now that you've deviated from Stroustrup, you've opened the door again to edit warring. Plus, we shouldn't cite Stroustrup anymore since the text doesn't support his version.
This debate over HW has gone on so long, I think I may just take this page off my watchlist and forget about it. ATren (talk) 10:56, 31 October 2008 (UTC)
I agree with ATren. Stroustrup, being the father of C++, is the best source of a Hello World example. One person's "subtlety" is another person's appropriate succintness, and attempting to debate the details of the example is just going to leave certain people unstatisfied. If you're going to change it, you need to provide a good source that is arguably as good as Stroustrup. Krelborne (talk) 22:45, 4 November 2008 (UTC)
I second that. We can debate the "best" Hello World until we're blue in the face. There's little debate that Stroustrup is a reliable source of C++ information and theory. OhNoitsJamie Talk 01:34, 5 November 2008 (UTC)
While I can see an argument for letting the wiki process freely evolve the example text to a new version, I tend to agree that Stroustrup's version might still be better than anything else we can agree on. SHEFFIELDSTEELTALK 14:45, 5 November 2008 (UTC)
Then please add a note forestalling the inevitable confusion by saying that the only function in C++ with an implicit return is main() (and my personal guess is that it is this way only for compatibility with pre-C99 C). Miqademus (talk) 19:24, 5 November 2008 (UTC)
I've tried to make the footnotes a bit clearer. SHEFFIELDSTEELTALK 19:45, 5 November 2008 (UTC)

reorganization of lead

I re-hashed the stuff on the lead[1]. First all the historical things, then the "significance" bits with the importance in industry, then the technical differences with C, and finally the techno-babble about its characteristics.

This should make the lead more digestable for non-technical people who come here to learn wtf C++ is, and still leave all the technical stuff. --Enric Naval (talk) 19:15, 7 November 2008 (UTC)

Languages which C++ influenced

There don't seem to be any sources given for the influences (original research perhaps). Also, the recent edit giving dates of languages is probably misleading, e.g., for Perl whose object-oriented features came "later". Tedickey (talk) 15:53, 28 December 2008 (UTC)

// my first programme inc++ —Preceding unsigned comment added by 124.43.46.228 (talk) 04:03, 13 February 2009 (UTC)

.cpp links here

.cpp redirects here, some mention should be made of what these files are. Mathiastck (talk) 18:34, 21 April 2009 (UTC)

Looking at other similar redirects (e.g. .c) no mention is made of specific file extensions. It seems to me that it would be innappropriate to include an info in this article, if only because it is bound to cause some controversy in the long run and bears little relation to the real subject matter of the article . —Preceding unsigned comment added by Cubathy (talkcontribs) 23:52, 21 April 2009 (UTC)


Yet another (better?) hello world program (and reasons for using it rather than anything that resembles Stroustrup's version)

The following version of the hello world program is rather different from the traditional version -- and with good reason. The traditional version does not in any way represent a typical C++ program, in that it does not contain any user defined types of any sort (class, struct or typedef) -- no attempt of calling a user defined method on an object is used either. While C++ is not strictly an object oriented programming language, it does support object oriented programming rather well and in my experience, except for old C programmers, people treat it as if it is a purely object oriented programming language. Therefor I consider the following hello world program worthy of recommendation (and usage) as a general introduction to C++ -- in fact I consider the versions that resemble Stroupstrup's version to be quite harmful to aspiring C++ programmers.

#include <iostream>

// declare a class named hello
class hello
  {

  };

// overload the << operator on ostreams, when given a hello object.
std::ostream& operator << (std::ostream& o, const hello& h)
  {
    return o << "Hello, World!";
  }

// main program
int main(int argc, char** argv)
  {
    // create an object of the type hello, with the name h
    hello h;
    // use the overloaded << operator on cout, with the argument h, which is a hello object.
    std::cout << h << std::endl;
    // tell the operating system that everything went ok.
    return 0;
  }

The usual debates about \n over endl and using namespace std over std::cout as well as the one on returning a value or not, do not apply. The point of this example to to introduce a new programmer to some of the object oriented features of C++ and to avoid a C like structure to the program (i.e. non-object oriented).

The above program does have a slightly increased complexity over the traditional version, but it has the upside of introducing new programmers to the features they will be using from the start, rather than teaching people C in C++ syntax. FrederikHertzum (talk) 13:17, 21 May 2009 (UTC)

personally, I find Stroustrup's hello world to be a terrible teaching example, and he really isn't qualified as a teacher, so his opinion on the best way to teach the system isn't necessarily authoritative, even though he was the primary designer of C++. however, that being said, the above example involving ostream operator overloading is, in my opinion, a horrible example, and it isn't specifically object oriented at all. in fact, it's as hackish, as confusingly specific to C++ semantics, as un-object-oriented, and as uninformative as Stroustrup's example. it fails in the same way to achieve the obvious goal of presenting a simple universal concept in a clear, concise, and inherently self-descriptive way, and it manages to depend on even more non-object-oriented, language-specific slight of hand.
Stroustrup's example was apparently designed to underscore language-specific hacks, which could be an effective way to demonstrate those peculiarities to a well-versed programmer who would understand the concepts, but it seems obvious to me that wikipedia's example should specifically not be designed to underscore subtle language semantics that are in no way universal or obvious concepts.
I think it's clear that no single hello world program will satisfy everyone, but if the article must have a hello world program then I think it would be a good idea to take one from a printed book by someone who is trained and well recognized as a teacher and who specializes in teaching programming languages. -无氏- 18:02, 21 May 2009 (UTC)
The problem with taking something from a printed book is that they all (afaik) take Stroustrup's example or one that is essentially the same as and copies it without consideration to what is actually being taught -- I have not read alot of beginners C++ books however so it is quite possible that some books do have better examples. So, that (unless I'm mistaken) is not going to happen any time soon. In what way do you think it is hackish? The only problem I see is the operator<< prototype, everything else is more or less selfexplanatory -- at least to the same extent as any other attempt I have seen.
I utterly fail to see the problem with my example -- what exactly do you think is non-object oriented about it? What language-specific slight of hand is involved? How does it fail to represent a simple universal concept and what concept? What is so hackish about it? And finally, why is it a problem that a hello world program, ment to introduce people to C++, is written in C++?
I will agree with you that the use of the overloading of the << operator is unfortunate but I don't see any real problems with it and even if it is unfortunate the example, at least, illustrates what C++ programs look like a lot better than the traditional examples.

80.167.70.76 (talk) 00:48, 22 May 2009 (UTC)

The problem with taking something from a printed book is that they all (afaik) take Stroustrup's example or one that is essentially the same as and copies it without consideration to what is actually being taught -- I have not read alot of beginners C++ books however so it is quite possible that some books do have better examples. So, that (unless I'm mistaken) is not going to happen any time soon. - I don't understand. are you saying that all teaching examples are invalid because, according to you, they're all be based on Stroustrup's hello world and would fail to consider the nature of C++? -无氏- 01:53, 22 May 2009 (UTC)
In what way do you think it is hackish? - C++ forces you to use a procedural entry point, so you're hacking in an unused class to make a simple program superficially OO where it's fundamentally procedural. also, the operator overload is a procedurally based hack to install a procedure in a class, which is also a library extending hack in the first place. -无氏- 01:53, 22 May 2009 (UTC)
what exactly do you think is non-object oriented about it? - the object is empty, it's not really an object at all, the method that does the printing is a procedural function, and the concept of mathematical operators isn't inherently or syntactically object oriented. -无氏- 01:53, 22 May 2009 (UTC)
What language-specific slight of hand is involved? How does it fail to represent a simple universal concept and what concept? - the language specific slight of hand is in the semantics relating to procedurally-based operator overloading and the association between the overloaded << operator and the standard library. for instance, does the reader know that cout is an ostream and that the behaviour of its << operator has been modified? it fails to directly and clearly express the concept of writing "hello world" to a terminal, instead expressing unrelated concepts that are unique to C++. -无氏- 01:53, 22 May 2009 (UTC)
And finally, why is it a problem that a hello world program, ment to introduce people to C++, is written in C++? - it's C++ in either case, whether you use procedural techniques to shoehorn in a user-defined class or not. -无氏- 01:53, 22 May 2009 (UTC)
First of all, current "Hello world" example follows the sentence "C++ inherits most features of C", so it makes sense as an illustration of that. However, I actually think we don't need a hello world in this article at all! If we must have an example of C++ and not of "most features of C", it should be something short yet cool looking, with STL and objects. --Cubbi (talk) 10:55, 22 May 2009 (UTC)
hello world should display "hello world" according to direct, minimal, intuitive best practices for displaying a line of text. a hello world program isn't a showcase for cherry picked language features that have nothing to do with that goal. that's why Stroustrup's example is a poor hello world, it aims to underscore language semantics rather than providing the simplest, most comprehensible program possible. -无氏- 15:43, 22 May 2009 (UTC)
You missed the point of my question: why do we need a program for displaying a line of text on a page about C++? It's not a page on displaying lines of text. C++ is not a language designed to display lines of text. The current example fits the flow of the page, even though it could do without it. Example program is good when it shows what the language was designed for. Your example would show a little bit of the language semantics, but it is utterly meaningless. What kind of objects are objects of class "hello"? --Cubbi (talk) 16:49, 22 May 2009 (UTC)
it's not my example. someone else posted it above and I commented that it's a horrible example. a hello world program is a well established common practice to demonstrate a way to perform a very common task in a given language. though, that doesn't mean that the article has to have a hello world program, but "It's not a page on displaying lines of text. C++ is not a language designed to display lines of text." isn't a meaningful argument against. the obvious reason to opt against having a hello world program would be that C++ is a confusing mixture of procedural and object oriented programming, that it's loaded with arbitrary semantics, that it is therefore impossible to express an intuitive and obvious hello world program in C++, and that trying to have one will cause an unending series of conflicts. -无氏- 18:37, 22 May 2009 (UTC)
C++ is a moreso than not, a procedural language, which supports object oriented programming. It is not an object oriented programming language. Most people do write programs in exactly that style -- the procedural nature of C++ if often more evident than the object oriented part. If you write hello world programs which is not ment to show anything about the language, why even do it? And, no, I do not believe that writing the simplest possible hello world programs is always the best thing to do. For C, it may make sense, but I do not believe that is the case for far more complex languages -- like C++.
I think the problem may be a more pedagogical one than it may seem -- I want people to know what they are dealing with as soon as possible, where as you seem to want them to learn at a slower pace. Regardless of the problems in C++, a hello world program should be present and it should represent the language and how it is used. C++ may not have been designed to work with text, but that doesn't mean it's particularly bad at it (compared to C, C++ is pure Python), even if it's not good at it.
In C++, you do use overloading, you do use classes and you do use streams. In that resepect, you can't find a hello world that's much better than what I proposed. You may consider adding inheritance, but that's better saved for a second lesson (IMHO).FrederikHertzum (talk) 16:18, 24 May 2009 (UTC)
Regardless of reasoning, wikipedia is not a textbook. Has your code been published? What makes it notable? --Cubbi (talk) 18:43, 24 May 2009 (UTC)
FrederikHertzum, C is a procedural language with no direct support of object orientation, and C++ is a superset of C, but the syntax that is specific to C++ is primarily about OO. in response to I want people to know what they are dealing with as soon as possible, where as you seem to want them to learn at a slower pace, please try to be civil and assume good faith. I would like to hope that all editors are aiming to inform the reader as effectively and efficiently as possible. also, C++ operator overloading (not to be confused with function overloading or overriding) is not a widely used feature, and it's not something that a majority of languages support, so it wouldn't seem to be a logical addition to a sample program that is intended to show programmers how a simple universal concept is expressed in C++. though, I think that C++ operator overloading and most major language features warrant a mention in the article, just not in the hello world program, because a hello world program isn't a generalized teaching tool for presenting random language features. -无氏- 20:46, 24 May 2009 (UTC)
无名氏 if I have not come across as being civil I apologize, I merely stated my impressions. I will try to act more civil, as you have suggested. I was assuming good faith, which was why I wrote you seem to want them to learn at a slower pace -- had I not assumed good faith, I would have written you want them to learn at a slower pace. Because I was assuming good faith I also asked you to clarify your critique of my proposal, rather than turn (really) ugly -- something I am still waiting for.
Cubbi my example is notable in that it is designed to introduce the reader to key elements of the C++ language rather than to pretend that C++ is a superset of C (which it is not, even if it is close). This is attempted by actually declaring a class, adding ostream support for that class and finally to show how to put them together. Even if Wikipedia is not a text book, a hello world example which is more akin to C programs than to C++, is a very poor representation of the C++ language. IMHO having no example is preferable to an example that does not show what distinguishes C++ from (in particular) C. The current de facto standard hello world program for C++, in my opinion, does not do so suffeciently.FrederikHertzum (talk) 01:20, 25 May 2009 (UTC)
well it's moot either way. established consensus is that a referenced hello world program by a published expert is to be used. -无氏- 01:42, 25 May 2009 (UTC)
Consensus exists to be changed. I can't seem to find any previouse reference to the fact that it has to be a hello world by a published expert anywhere -- please provide a citation.FrederikHertzum (talk) 04:40, 25 May 2009 (UTC)
Using google, I have found two pages which sport the same class based hello world program in C++. It is not the same as the one I wrote, but it is suffeciently similar that I will draw your attention to it. What appears to be the original is located here: [2]
Since you did not like having an empty class, would the following better suit your taste?
#include <iostream>
class hello
  {
    public: operator const char* () { return "Hello World!"; }
  };

int main()
  {
    hello h;
    std::cout << h << std::endl;
  }

FrederikHertzum (talk) 05:04, 25 May 2009 (UTC)

much better, in that it is closer to being an object by definition, and it uses object oriented syntax. it fails the test of being minimal, though, and it utilizes cast overloading which isn't a universal concept in programming, or something that a beginner would find obvious and intuitive. the previous discussions about a hello world program made it clear that the basis of using Stroustrup's example wasn't the content of the example, but the fact that it was a published example from a book by an expert. see the unending discussion in previous sections on this talk page. -无氏- 16:19, 25 May 2009 (UTC)
Wether or not casting is a universal concept in programming or not is really not an argument against this example being a good example for C++ or not -- the example is not an example on object oriented programming, but on C++ programming. An alternative to the hello world example above could be implementing a print method, which would then be called -- the version in the example I linked to is not a good example in my opinion however, since it does not allow redirecting the output to anything but standard output. Doing it differently could only increase the example, not reduce it -- save for doing it the Stroustrup way.
I would argue that my example is minimal (or at least close to it), since it is hardly possible to reduce it and keep objects and classes in the example. std::cout (and similar mechanisms), I would argue is not part of the example -- it is simple the only way to actually get the result printed to the user. Besides, the program is not designed to be a minimum C++ program, but a minimum program that illustrates classes in C++, being perhaps, one of the most important differences from C and similar languages.
I think it is vital that C++ is always distinguished from C, since the two languages are not fully compatible and should you ever program C++ in the same way you do C, you are either overengineering your C programs or doing something very wrong in your C++ programs. This is of course a personal opinion that's rather hard to prove, given the subjective nature of the argument.
Given that this is an article on C++, I believe that it should contain C++ code that illustrates typical usage and not focus on what has been published or not -- in my experience teachers and writers seldom write programs similar to how typical code is written.

FrederikHertzum (talk) 21:57, 1 June 2009 (UTC)

#include <iostream>
class hello
  {
  public: hello( const char* p ) { std::cout << "Hello " << p << "!"; }
  };
 
int main()
  {
    hello( "world" );
  }

Well, this is fun, isn't it? I wonder how we're going to decide whose version is best, though. SHEFFIELDSTEELTALK 21:35, 1 June 2009 (UTC)

That's easy, since your example is actually more complex than mine and it doesn't actually create a variable to hold your object (but that's a very minor thing). That being said, I wouldn't object to your example at all. The point is the declaration and use of a class in the program, which your example does (was this not clear?). FrederikHertzum (talk) 22:05, 1 June 2009 (UTC)

or, to shoehorn an even more complete object into a program that should just print a line as directly and simply as possible:

#include <iostream>

class shoehorned
  {
  char const* _text;
  public:
  void print() { std::cout << _text; }
  shoehorned(char const* text) : _text(text) { }
  };
 
int main()
  {
    shoehorned("hello world\n").print();
  }

-无氏- 22:18, 1 June 2009 (UTC)

Criticisms sections are good!

I think this article will profit from retaining the criticism section rather than redistributing the criticism to be disolved in the rest of the text. I think criticism sections are preferrable in all programming language articles, so that the criticisms and rebuttal arguments are collected. However, putting a "See also: Comparison of Java and C++" first in the criticism section is pathetical, since Java in no way distinguishes itself by solving C++'es serious problems (such as PODs and parallel programming, vague language definitions up to and including the template mess), in fact C++ and Java doesn't even direct towards the same application areas, and so cannot replace each other. ... said: Rursus (bork²) 14:11, 15 June 2009 (UTC)

I would much rather it were talking about C++ vs C#, because, while the comparison between C++ and Java is quite relevant in that section, I can't bring myself to say anything good about that vendor-specific, feature-starved language. so I'll just respond as if C# were the subject:
C# does address many of C++'s failings. for example, C# provides total containment, in that C# code can't corrupt or leak memory, and a given routine can't access any objects without having explicit permission to do so. also, C#'s generics (which were eventually cloned in Java) are specifically designed to replace C++ templates with a less opaque and chaotic system. also, C# delegates (not part of Java) provide a much simpler way of doing a lot of what C++ pointer-to-member-function syntax does, in addition to providing support for static function pointers. C# also provides some new functionality that C++ lacks, for example, lambda expressions, properties, object initializers, and more. C# also solves a lot of C++'s limitations as a parallel/distributed language which still go unaddressed by C++0x, for example, using references rather than full pointers enables efficient and reliable garbage collection which isn't possible in C++, and that in turn eliminates confusing ownership scenarios where it can be hard or impossible to figure out which thread is the last to use an object and is therefore responsible for deleting it. also, C# has built-in mutually exclusive locking, asynchronous delegates, and other features that make threaded programming a lot easier. also, C# is pretty much a complete alternative to C++, it can even be used for embedded programming on PICs with 100 bytes of RAM, and Microsoft is planning to rewrite the entire Windows OS in C#, including the kernel, as part of their .NET initiative. they've already produced a small research OS written almost entirely in C#. part of the reason for this is that code written in C# isn't subject to buffer overflows and other issues like code written in C++ using pointers, so security issues would be greatly reduced in number and severity by using C# instead of C++. -无氏- 18:09, 15 June 2009 (UTC)
I agree with keeping the criticism section and perhaps C# is indeed a better comparison than Java. However, this is supposed to be an article about C++ and this part of the article is supposed to highlight C++'s criticisms, not how brilliant you think C# is. If you're going to make this comparison, tidy up the weasel words and revise the text to be critical of C++, not praising C# for avoiding some of the pitfalls.--217.34.44.238 (talk) 11:46, 18 August 2009 (UTC)
The problem with comparison to Java and C# (or a lot of other languages for that matter) is that those languages are all running in a VM, while C++ does not, so a comparison would mostly yield the difference between a VM'ed and a non-VM'ed language. If we should compare against another language, we choose a non-VM'ed, objected-oriented, statically typed language, otherwise we will just be comparing those basic choices against one another. Candidates include Objective C and Eiffel. Esben (talk) 20:23, 8 November 2009 (UTC)