Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

test

5 views
Skip to first unread message

Alf P. Steinbach

unread,
Jan 23, 2009, 4:27:43 PM1/23/09
to
cout << "hah!\n";

Anonym Gjřk

unread,
Jan 24, 2009, 3:36:17 AM1/24/09
to
"Alf P. Steinbach" <al...@start.no> writes:

> cout << "hah!" << endl;

Anonym Gjřk

unread,
Jan 24, 2009, 4:08:15 AM1/24/09
to
"Alf P. Steinbach" <al...@start.no> writes:

#include <iostream>
#include <string>

class Hah {

private:
std::string msg;

public:
Hah() {
this->msg = "Hah!";
}
std::string hah() {
return this->msg;
}
};


int main() {
Hah hah;
std::cout << hah.hah() << std::endl;
return 0;
}


There. Fixed that for you.

Espen Myrland

unread,
Jan 24, 2009, 4:14:25 AM1/24/09
to
anonymou...@cuckoo.no (Anonym Gjøk) writes:

> "Alf P. Steinbach" <al...@start.no> writes:
>
> > cout << "hah!" << endl;


Flyttet til no.test

Alf P. Steinbach

unread,
Jan 24, 2009, 6:23:55 AM1/24/09
to
* Anonym Gjøk:

Thanks.

An idiomatic version of that program would be something like this:

<code>
#include <iostream>
#include <string>

class Hah
{
private:
std::string myMsg;
public:
Hah(): myMsg( "Hah!" ) {}
std::string hah() const { return myMsg; }
};

int main()
{
std::cout << Hah().hah() << std::endl;
}
</code>

Differences / best practice guidelines:

* Use initializer lists rather than assignment in constructor body.

* Add 'const' wherever practically possible.

* In C++ 'main' has a default result value, namely 0, and best practice
is to avoid redundant code that doesn't communicate significant
info to one reading the code (code is about communicating to programmers),
or has some other purpose such as supporting later modification.

In passing, if the text is changed from "Hah" to "Høh?" then the program will
still compile fine with MinGW g++ in Windows, and even yield the correct result
if the source code is written in Latin-1 and the program is run in the Windows
command interpreter with codepage 1252 (Windows ANSI Western, selected by e.g.
the chcp command), but this then is due to a *bug* in that compiler where it
doesn't detect that the source code contains invalid UTF-8 bytes.

Writing the source code in UTF-8 would be a workaround for that compiler bug,
but then the relevant Windows command interpreter codepage would be 65001, which
has not just one *bug* but a phletora of them; no output is guaranteed[1]...

In short, current C++ implementations for Windows fail the test of being able to
implement the Norwegian "Høh?" program without direct use of API functions, i.e.
using only standard core language and standard library functionality.

Unfortunately other programming languages do not fare very much better.

It's only 2009 -- we'll probably have to wait 30 years more for this basic
functionality to be in place...


Cheers & happy new year

- Alf


Notes:
[1] An interesting exercise in the Windows XP command interpreter:
C:\> chcp 65001
C:\> more

0 new messages