Saturday, November 1, 2008

25 new messages in 11 topics - digest

comp.lang.c++
http://groups.google.com/group/comp.lang.c++?hl=en

comp.lang.c++@googlegroups.com

Today's topics:

* Strooustrup - Hello World exercise - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/082118a58f35476a?hl=en
* The C++ Object Model: Good? Bad? Ugly? - 3 messages, 3 authors
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/6e3c894e0875a337?hl=en
* preprocessor and character literal ('#letter') - 4 messages, 4 authors
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/1066761954e0b6b0?hl=en
* Template methods with function pointers - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/c8914476cce1d9f8?hl=en
* Sorting a list Structure - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/82cc848a4d1e245a?hl=en
* Coming back from C to C++ - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/6a5d13f31fd6825d?hl=en
* typedef Syntax Error - 4 messages, 3 authors
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/6929a253194f7de0?hl=en
* a problem about "sqrt" - 4 messages, 4 authors
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/a2084ac75a5823f4?hl=en
* elementary string processing question - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/5477a6698fdf8341?hl=en
* Erasing from middle of a list problem - 3 messages, 3 authors
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/e74fcbbad6312ec6?hl=en
* Class templates and singleton container - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7a8d15d1f932b3df?hl=en

==============================================================================
TOPIC: Strooustrup - Hello World exercise
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/082118a58f35476a?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Oct 31 2008 9:07 pm
From: blargg.h4g@gishpuppy.com (blargg)


In article
<491f5749-f52b-4614-ab2d-83e0741be7b1@k36g2000pri.googlegroups.com>,
gw7rib@aol.com wrote:

> On 30 Oct, 20:41, arnuld <sunr...@invalid.address> wrote:
> > How about this:
> >
> > #include <iostream>
> >
> > class Useless
> > {
> > public:
> > =A0 Useless() =A0{ std::cout << "Initialize" << std::endl; }
> > =A0 ~Useless() { std::cout << "Clean up" =A0 << std::endl; }
> >
> > };
> >
> > Useless obj_of_useless;
> >
> > int main()
> > {
> > =A0 std::cout << "Hello World!" << std::endl;
> >
> > =A0 return 0;
> >
> > }
>
> Yes, that looks just the sort of thing they were after.

Unlike this questionable solution that defines no new classes:

#include <iostream>
#include <cstdlib>

namespace
{
void cleanup() { std::cout << "Clean up\n"; }

int init = (std::cout << "Initialize\n", std::atexit( cleanup ));
}

int main() { }


==============================================================================
TOPIC: The C++ Object Model: Good? Bad? Ugly?
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/6e3c894e0875a337?hl=en
==============================================================================

== 1 of 3 ==
Date: Sat, Nov 1 2008 1:12 am
From: ebony.soft@gmail.com


On Nov 1, 6:02 am, tonytech08 <tonytec...@gmail.com> wrote:
> What I like about the C++ object model: that the data portion of the
> class
> IS the object (dereferencing an object gets you the data of a POD
> object).
>
> What I don't like about the C++ object model: that most OO features
> are not
> available for class object design without loss of POD-ness.
>
> So, I'm more than leaning toward "bad" because of the limitations and
> that the language doesn't distinguish what is of very key importance.
> How do you feel about the object model?

Hi
I really don't get completely, what you mean, but for me, the
important things about C++ object model are simplicity, efficiency and
extenability. For example the representation of concrete classes like
Date in memory is exactly like a POD Date struct. The size of object
of a derived class is the size of base class sub-object and its data
members. The size of an object of a class with virtual functions
increases just by the size of virtual v-ptr. The above layout is
extended for classes with static members, multiple inheritance,
virtual base classes and RTTI.
In all, you can see these three items.

I hope it helps you.

Regards,
Saeed Amrollahi

== 2 of 3 ==
Date: Sat, Nov 1 2008 2:32 am
From: James Kanze


On Nov 1, 4:02 am, tonytech08 <tonytec...@gmail.com> wrote:
> What I like about the C++ object model: that the data portion
> of the class IS the object (dereferencing an object gets you
> the data of a POD object).

No it doesn't.

> What I don't like about the C++ object model: that most OO
> features are not available for class object design without
> loss of POD-ness.

> So, I'm more than leaning toward "bad" because of the
> limitations and that the language doesn't distinguish what is
> of very key importance. How do you feel about the object
> model?

The "object model" in the C++ standard is very low-level.
Intentionally. It is designed for you to build on. The object
model of the classes you write is for you to design. It can be
more OO than many other languages (e.g. Java or C#), when that's
appropriate. Just as it can drop the OO model completely when
that's appropriate (e.g value objects). The essential point of
C++ is that it doesn't impose any application level object
model; it lets you use whatever is appropriate.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

== 3 of 3 ==
Date: Sat, Nov 1 2008 3:41 am
From: Salt_Peter


On Oct 31, 10:02 pm, tonytech08 <tonytec...@gmail.com> wrote:
> What I like about the C++ object model: that the data portion of the
> class
> IS the object (dereferencing an object gets you the data of a POD
> object).

Definitely not. Data is just data. The true power of the C++ Object
Model is the object's behaviour and extensability.

>
> What I don't like about the C++ object model: that most OO features
> are not
> available for class object design without loss of POD-ness.
>
> So, I'm more than leaning toward "bad" because of the limitations and
> that the language doesn't distinguish what is of very key importance.
> How do you feel about the object model?

Objects sometimes need to do more than just store data. Behaviour is
nice to have. That inludes 'behaviour' at construction and expected
behaviours through a secure interface. It makes maintaining and
extending code easy, simple.

Write a program that uses some given polymorphic type hierarchy and
have your customer invent / add some new improved class of his own.
The customer's new class works with your original program without
changing a single line of code (except maybe an include).

You can't do that with PODs only. Whats nice is you can bend the
language to choose what path you prefer.


==============================================================================
TOPIC: preprocessor and character literal ('#letter')
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/1066761954e0b6b0?hl=en
==============================================================================

== 1 of 4 ==
Date: Sat, Nov 1 2008 1:32 am
From: "dascandy@gmail.com"


On Nov 1, 1:00 am, Boris Dušek <boris.du...@gmail.com> wrote:
> #define LETTER_STRAIGHT(let) let = L'#let'
>
<snip>
>
> enum Letter {
>     A = L'#let',
>     B = L'#let',
>     C = L'#let',
>
> };

You could try

#define LETTER_STRAIGHT(let) let = L#let[0]

which should create
> enum Letter {
> A = L"A"[0],
> B = L"B"[0],
> C = L"C"[0],
>
> };

which should be functionally equivalent. You can't create a single
char with preprocessor macros - it's always a string. You can then use
the first char of that string, iirc.

== 2 of 4 ==
Date: Sat, Nov 1 2008 1:57 am
From: James Kanze


On Nov 1, 9:32 am, "dasca...@gmail.com" <dasca...@gmail.com> wrote:
> On Nov 1, 1:00 am, Boris Dušek <boris.du...@gmail.com> wrote:

> > #define LETTER_STRAIGHT(let) let = L'#let'

> <snip>

> > enum Letter {
> >     A = L'#let',
> >     B = L'#let',
> >     C = L'#let',
> > };

> You could try

> #define LETTER_STRAIGHT(let) let = L#let[0]

> which should create

> > enum Letter {
> >     A = L"A"[0],
> >     B = L"B"[0],
> >     C = L"C"[0],
> > };

> which should be functionally equivalent. You can't create a
> single char with preprocessor macros - it's always a string.
> You can then use the first char of that string, iirc.

You can, but the result of a dereferce operator is never a
constant expression, even if you're dereferencing a string
literal, so it can't be used as the initializer of an enum
constant (or an array dimension, or a template argument).

There are a number of different work-arounds possible, but
without knowing what problem he's trying to solve, it's
difficult to recommand any. Off hand, I don't see what the
problem is in writing:

enum Letter
{
A = L'A',
B = L'B',
C = L'C'
} ;

directly.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

== 3 of 4 ==
Date: Sat, Nov 1 2008 2:49 am
From: Juha Nieminen


James Kanze wrote:
> Off hand, I don't see what the
> problem is in writing:
>
> enum Letter
> {
> A = L'A',
> B = L'B',
> C = L'C'
> } ;
>
> directly.

Maybe he is trying to abstract the precise character type (L) away?

== 4 of 4 ==
Date: Sat, Nov 1 2008 3:09 am
From: Boris Dušek


> > #define LETTER_STRAIGHT(let) let = L#let[0]
> > which should create
> > > enum Letter {
> > >     A = L"A"[0],
> > >     B = L"B"[0],
> > >     C = L"C"[0],
> > > };
> > which should be functionally equivalent. You can't create a
> > single char with preprocessor macros - it's always a string.
> > You can then use the first char of that string, iirc.
>
> You can, but the result of a dereferce operator is never a
> constant expression

Right, the compiler complains about that.

> difficult to recommand any.  Off hand, I don't see what the
> problem is in writing:
>
>     enum Letter
>     {
>         A = L'A',
>         B = L'B',
>         C = L'C'
>     } ;
>
> directly.

I had to write something like 100 enum values, so having a macro that
saves typing (if you copy/paste the line 100 times and then just
change one letter) and that helps avoiding a mistake that would result
from forgetting to change the right-hand side when I changed the left-
hand side is a plus. But I used vim to easily change it to the
explicit form, now it of course works.

What I am doing is that I want to include Unicode codepoints for all
letters in Czech alphabet (i.e. asides from English alphabet, 15 more
like ì¹èø¾ýáíé) into the enum, and then define uppercasing,
lowercasing and asciifying (é -> e) tables using these enum values
rather than the codepoints directly. I want to avoid relying on cs_CZ
or Czech locale, since i.e. Mac OS X's support for locale is basically
non-existent (i.e. even std::locale("") throws std::runtime_error; the
same for std::locale("cs_CZ") or any other even if it is present in /
usr/share/locale). So I have to have my own conversion tables. And
btw., the goal of all of this is to write an app that tries its best
to deasciify an asciified text.

Thanks to both of you for your ideas,
Boris


==============================================================================
TOPIC: Template methods with function pointers
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/c8914476cce1d9f8?hl=en
==============================================================================

== 1 of 1 ==
Date: Sat, Nov 1 2008 1:35 am
From: "dascandy@gmail.com"


On Oct 31, 6:24 am, cande...@gmail.com wrote:
> I did get it to compile with this
>
> template<TItemData>
> LONG __cdecl FilterListProc(TItemData & Item1,TItemData & Item2,LPVOID
> pParam = NULL);
>
> template<class TItemList,class TItemData>
> class CFiltredListImpl
> {
>         private:
>         class vaiables anf methods
>         ...
>         public:
>         BOOL Filter(LONG (__cdecl *FilterProc)(TItemData &,TItemData
> & ,LPVOID),LPVOID lpPram = NULL)
>         {
>                 return TRUE;
>         };
>
> };
>
>

The second misses an ampersand in front of the function name, I'm not
sure if that's all.

The third misses both the ampersand and the template instantiation
parameters, which means that you can't pass the plain template - you
have to pass it as an instantiated function:

BOOL Filter(LONG (__cdecl *FilterProc)(TItemData &,TItemData
& ,LPVOID) = &FilterListProc<TItemData>,LPVOID lpPram = NULL)

which should work.

Good luck.


==============================================================================
TOPIC: Sorting a list Structure
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/82cc848a4d1e245a?hl=en
==============================================================================

== 1 of 1 ==
Date: Sat, Nov 1 2008 1:38 am
From: James Kanze


On Oct 31, 9:58 pm, mrc2...@cox.net (Mike Copeland) wrote:
> > >    I'm confused about how to sort a list on an element of the list's
> > > structure.  Here is my data definition:

> > > struct Finishes               // computed Finish information
> > > {
> > >         long  netTime;
> > >         int   bibNumber;} fWork;

> > > typedef list<Finishes> FINISHES;
> > >         FINISHES finishInfo;
> > >         list<Finishes>::iterator fIter;

> > > I have populated the list successfully, and now I want to
> > > sort the list on the netTime element.  All examples of the
> > > list.sort I've found work only with scalars or
> > > single-element structures.  How do I do this with my data?
> > > TIA

> > Why not use an appropriate container like std::map where
> > netTime is the key and the integer its values? Elements are
> > then sorted on insertion using the default comparator
> > less<>.

> I don't have a need for that.  My application just produces
> Times & assigns them to bibNumbers; I just need a dynamic
> array of such data (a list is fine), and then I sort the data
> on Time.  I don't need to access any object members randomly,
> because once I've sorted the array I will process all objects
> in that sequence.

Actually, unless you need to insert at random locations in the
middle, std::vector or std::deque are probably more appropriate;
I'd probably go with std::vector in your case, since you can do
all your insertions at the end. std::map and std::set become
interesting when you have to maintain order with ongoing
insertions.

Anyway, as others have probably already said (I haven't seen the
other responses), all you need is to define the appropriate
comparator. Something like:
struct CompareTimes
{
bool operator()(
Finishes const& lhs,
Finishes const& rhs ) const
{
return lhs.netTime < rhs.netTime ;
}
} ;
and pass an instance of this to the appropriate sort routine.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


==============================================================================
TOPIC: Coming back from C to C++
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/6a5d13f31fd6825d?hl=en
==============================================================================

== 1 of 1 ==
Date: Sat, Nov 1 2008 1:39 am
From: "dascandy@gmail.com"


On Oct 30, 4:08 pm, Tomás Ó hÉilidhe <t...@lavabit.com> wrote:
> Currently what's the biggest integer type in C++? Does C++ have all
> the <stdint.h> types such as uint_fast64_t? I use these types a lot.
> Back when I did C++ programming a few years ago, I don't think all C++
> implementations were guaranteed to have <stdint.h>. Have things
> changed? Also, does C++ have the "long long" integer type that's
> guaranteed to be at least 64-Bit?

Not yet, and not yet. Both should be added in c++09, which will be (as
you can tell) next year.

> Another thing: Can anyone suggest what's the most portable networking
> library for sending and receiving raw Ethernet frame? In the world of
> C, "Berkeley Sockets" seems to be the main one. What about C++, what's
> the best networking library to use if you're looking to maximise
> portability? (Remember that I need to be able to send and receive full
> Ethernet frames).

Berkeley sockets or pcap-derived things. I think berkeley sockets are
the more portable one.

> And one last thing: My program won't have a fancy GUI, it'll just be a
> console application. I realise that neither the C nor C++ standard
> libraries provide fancy facilities for setting the text colour, but
> I'm wondering is there a portable library out there for doing this? I
> recall using "conio.h" a few years ago but I don't know if this is the
> "de facto" standard.

That depends on the kind of console somebody has. You could use a
library like ncurses to wrap that for you or you can do it yourself.


==============================================================================
TOPIC: typedef Syntax Error
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/6929a253194f7de0?hl=en
==============================================================================

== 1 of 4 ==
Date: Sat, Nov 1 2008 1:49 am
From: James Kanze


On Oct 31, 10:16 pm, Victor Bazarov <v.Abaza...@comAcast.net>
wrote:
> red floyd wrote:
> > On Oct 31, 2:01 pm, mrc2...@cox.net (Mike Copeland) wrote:
> >> I'm getting a syntax error on the "typedef" code line here.
> >>  Any thoughts on why?  TIA

> >> struct CSTYPE
> >> {                                              // City/State Record
> >>         string csKey;                            // City/State "Key"
> >>         string csCity;                                       // City
> >>         string csState;                                // State Code};

> >> typedef map<string, CSTYPE> CSINFO;  // <=== error here
> >>         extern CSINFO cityStInfo;
> >>         extern map<string, CSTYPE>::iterator csIter;
> >>         extern CSTYPE workCS;

> > 1.  did you #include <map> and <string>?
> > 2.  map and string live in the std:: namespace
> > 3.  Would you care to describe the specific error?

> It's possible that his compiler does not allow the use of
> incomplete types as template arguments, even in typedefs, and
> 'CSTYPE' is incomplete at that point...

That wasn't the case in his original code. If you'll look at
the end of the line "string csState;", you'll see the closing
brace of CSTYPE. For some reasons, something in the news very
often seems to move a line with just a }: to the end of the
preceding line. (I've always suspected Google, because that's
what I use to read news, and I'm constantly seeing this.)

Without the closing brace, of course, the code would be illegal.
Because of the incomplete type, but also because you're not
allowed to use extern in a class either (and that error requires
a dignostic).

Like Mike, I rather suspect that he's either forgotten the
include, and he's clearly fogotten the std::. Which means that
string and map are unknown symbols. To reasonably parse C++,
the compiler needs to know when a symbol names a type; use an
unknown symbol where a type is required, the compiler will
generally suppose it isn't a type (which results in a syntax
error), and the quality of the error messages go downhill from
there.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

== 2 of 4 ==
Date: Sat, Nov 1 2008 1:52 am
From: James Kanze


On Oct 31, 10:43 pm, mrc2...@cox.net (Mike Copeland) wrote:
> > >>    I'm getting a syntax error on the "typedef" code line here.  Any
> > >> thoughts on why?  TIA

> > >> struct CSTYPE
> > >> {                                              // City/State Record
> > >>         string csKey;                            // City/State "Key"
> > >>         string csCity;                                       // City
> > >>         string csState;                                // State Code
> > >> };  <==== this was in my post, but was appended to the above line.

> > It's possible that his compiler does not allow the use of
> > incomplete types as template arguments, even in typedefs,
> > and 'CSTYPE' is incomplete at that point...

> Yes, but please see my common above.  I _thought_ I had a
> complete declaration for CSTYPE - am I wrong?

You did. Your original posting was correct in this regard, but
something in the net seems to occasionally (often, in fact) join
a line with just a "}" or a "};" with the preceding line. If
the preceding line doesn't end with a comment, it doesn't affect
the legality of the code, but it is an unusual formatting. (I
asked about this once, since I was seeing so many postings with
this unusual formatting, and thought it might be some new coding
convention I wasn't familiar with. The poster in question
assured me that the }; was on a separate line when the posting
left his machine.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

== 3 of 4 ==
Date: Sat, Nov 1 2008 2:06 am
From: "Jim Langston"

"Mike Copeland" <mrc2323@cox.net> wrote in message
news:MPG.237529dc75782682989730@news.cox.net...
>> > I'm getting a syntax error on the "typedef" code line here. Any
>> > thoughts on why? TIA
>> >
>> > struct CSTYPE // City/State Record
>> > {
>> > string csKey;// City/State "Key"
>> > string csCity; // City
>> > string csState; // State Code
>> > };
>> > typedef map<string, CSTYPE> CSINFO; // <= error here
>> > extern CSINFO cityStInfo;
>> > extern map<string, CSTYPE>::iterator csIter;
>> > extern CSTYPE workCS;
>>
>>
>> 1. did you #include <map> and <string>?
>> 2. map and string live in the std:: namespace
>> 3. Would you care to describe the specific error?
>
> I didn't state the error (C2143) because it's compiler-specififc
> (VC++ 6.0) and I know that's a no-no here. 8<{{

There is no reason I know of to use VC++ 6.0 when you can download VC++ 8.0
for free from Microsoft which is much better and up to date. See here.
http://www.microsoft.com/express/download/

> I also didn't state that this is in a common .h file I'm building
> (and I apologize for not mentioning that...)

== 4 of 4 ==
Date: Sat, Nov 1 2008 4:34 am
From: "Bo Persson"


Jim Langston wrote:
> "Mike Copeland" <mrc2323@cox.net> wrote in message
> news:MPG.237529dc75782682989730@news.cox.net...
>>>> I'm getting a syntax error on the "typedef" code line here. Any
>>>> thoughts on why? TIA
>>>>
>>>> struct CSTYPE // City/State Record
>>>> {
>>>> string csKey;// City/State "Key"
>>>> string csCity; // City
>>>> string csState; // State Code
>>>> };
>>>> typedef map<string, CSTYPE> CSINFO; // <= error here
>>>> extern CSINFO cityStInfo;
>>>> extern map<string, CSTYPE>::iterator csIter;
>>>> extern CSTYPE workCS;
>>>
>>>
>>> 1. did you #include <map> and <string>?
>>> 2. map and string live in the std:: namespace
>>> 3. Would you care to describe the specific error?
>>
>> I didn't state the error (C2143) because it's compiler-specififc
>> (VC++ 6.0) and I know that's a no-no here. 8<{{
>
> There is no reason I know of to use VC++ 6.0 when you can download
> VC++ 8.0 for free from Microsoft which is much better and up to
> date. See here. http://www.microsoft.com/express/download/
>

That's VC9 even, telling us that VC6 is just ancient. Don't use it
unless someone is pulling your toenails out!


Bo Persson



==============================================================================
TOPIC: a problem about "sqrt"
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/a2084ac75a5823f4?hl=en
==============================================================================

== 1 of 4 ==
Date: Sat, Nov 1 2008 2:06 am
From: James Kanze


On Nov 1, 2:23 am, linlinfan...@163.com wrote:
> why it print wrong result? I can't find the wrong place.

> #include<stdio.h>
> #include<math.h>
>
> double distance(double a ,double b,double c,double d);
>
> int main()
> {
>    double x1,y1,x2,y2;
>    double result;
>
>    printf("Enter four numbers:");
>    scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
>
>    result=distance(x1,y1,x2,y2);
>
>    printf("The distancd is %.1f",result);
>
>    return 0;
> }

> double distance(double a,double b,double c,double d)
> {
>    return sqrt((a-c)*(a-c)+(b-d)*(b-d));
> }

The reason why it prints the wrong result is that you're using
scanf. Try doing it the C++ way:

#include <iostream>
#include <cstddef>
#include <cmath>

extern double distance( double a, double b, double c, double d ) ;

int
main()
{
std::cout << "Please enter four numbers: " ;
double x1 ;
double y1 ;
double x2 ;
double y2 ;
if ( std::cin >> x1 >> y1 >> x2 >> y2 ) {
std::cout << "The distance is: "
<< distance( x1, y1, x2, y2 )
<< std::endl ;
} else {
std::cerr << "Those weren't four legal numbers"
<< std::endl ;
}
return EXIT_SUCCESS ;
}

double
distance(
double x1,
double y1,
double x2,
double y2 )
{
return std::sqrt( ((x1-x2) * (x1-x2)) + ((y1-y2)*(y1-y2)) ) ;
}

You should just forget about stdio.h (except for a few things
like remove or rename); the interface is horribly broken, and
extremely error prone and unnatural. (The iostream interface
isn't without problems either, but it's still an order of
magnitude better than stdio.h.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

== 2 of 4 ==
Date: Sat, Nov 1 2008 2:53 am
From: Juha Nieminen


James Kanze wrote:
> double x1 ;
> double y1 ;
> double x2 ;
> double y2 ;

A question of style, but is it *really* necessary to be so verbose?
I honestly don't think this is any less clear (if anything, it's the
contrary):

double x1, y1, x2, y2;

== 3 of 4 ==
Date: Sat, Nov 1 2008 4:06 am
From: fungus


On Nov 1, 2:23 am, linlinfan...@163.com wrote:
> why it print wrong result? I can't find the wrong place.
>
>    double x1,y1,x2,y2;
>
>    printf("Enter four numbers:");
>    scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
>

Using %f in scanf doesn't read a double, it reads a float.

Best not to use scanf, it's a dangerous function (as you're
finding out...)

== 4 of 4 ==
Date: Sat, Nov 1 2008 4:39 am
From: "Bo Persson"


Juha Nieminen wrote:
> James Kanze wrote:
>> double x1 ;
>> double y1 ;
>> double x2 ;
>> double y2 ;
>
> A question of style, but is it *really* necessary to be so verbose?
> I honestly don't think this is any less clear (if anything, it's the
> contrary):
>
> double x1, y1, x2, y2;

Your style is only usable when declaring several uninitialized
variables of the same type. That makes it very unusual.

In all other cases, James' version is much better. Using it even for
this corner case, is at least more consistent.


Bo Persson



==============================================================================
TOPIC: elementary string processing question
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/5477a6698fdf8341?hl=en
==============================================================================

== 1 of 2 ==
Date: Sat, Nov 1 2008 2:28 am
From: James Kanze


On Nov 1, 4:28 am, tonywh00t <tony.s...@gmail.com> wrote:

> I have a "simple" question, especially for people familiar
> with regex. I need to parse strings that have the form:

> 1:3::5:9

> which indicates the set of integers {1 3 4 5 9}. In other
> words i have a set of numbers separated by ":", where "::"
> indicates a range from lo to hi inclusive. It is desirable to
> error check this string (i.e it should. start and end with a
> number, and be composed only numbers, "::", and ":"). I'm
> currently using the Boost C++ library, and i've worked out
> some pretty ugly solutions. If anyone has a suggestion, I'd
> very much appreciate it. Thanks!

I presume that the number of entries in the string may vary;
otherwise, of course, you said it yourself, regex. I'd still
use regex to validate the string, something like
"^\\d+(:\\d+|::\\d+)*$", I think would do the trick. (It would
be really elegant if you could use capture, but capture doesn't
work well within closures---only the last match is captured.)
Then I'd simply break the string up into substrings at each ':':

std::vector< std::string >
parse( std::string const& source )
{
typedef std::string::const_iterator
TextIter ;
std::vector< std::string >
result ;
TextIter current = source.begin() ;
TextIter const end = source.end() ;
while ( current != end ) {
TextIter fieldBegin = current ;
current = std::find( current, end, ':' ) ;
result.push_back( std::string( fieldBegin, current ) ) ;
if ( current != end ) {
++ current ;
}
}
return result ;
}

This gives you an array of strings, with an emtpy string between
:: (so when you see an empty string, you know you have a range).
So you could do something like:

int
toInt( std::string const& string )
{
std::istringstream cvt( string ) ;
int result ;
cvt >> result ;
return result ;
}

std::vector< int >
convert( std::vector< std::string const& source )
{
typedef std::vector< std::string >::const_iterator
FieldIter ;
std::vector< int > result ;
FieldIter current = source.begin() ;
FieldIter const end = source.end() ;
while ( current != end ) {
result.push_back( toInt( *current ) ) ;
++ current ;
if ( current != end && *current == "" ) {
int bottom = result.back() ;
++ current ;
int top = toInt( *current ) ;
if ( top <= bottom ) {
throw someError ;
}
while ( ++ bottom <= top ) {
result.push_back( bottom ) ;
}
++ current ;
}
}
sort( result.begin(), result.end() ) ;
// Or you might want to track the last seen to ensure
// that the input was correctly sorted.
return result ;
}

Note that all of the above code supposes the precheck on the
format using regex. Otherwise, you'll need a lot more error
handling and special cases.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

== 2 of 2 ==
Date: Sat, Nov 1 2008 3:01 am
From: Juha Nieminen


tonywh00t wrote:
> I'm currently using the Boost C++ library, and i've
> worked out some pretty ugly solutions. If anyone has a suggestion, I'd
> very much appreciate it. Thanks!

My experience is that whenever you need to parse input data which is
more complicated than fixed-format whitespace-separated elements, the
parsing code always becomes very complicated in C++ (as well as C). The
C/C++ language has clearly not been designed to be a language which you
can use to create complicated format parsers with one-liners. Often not
even with 100-liners (especially if you want full error checking).

Of course libraries have been developed during the decades to try to
help this, but they often only help more on the abstraction rather than
on the verbosity and complexity of the code.


==============================================================================
TOPIC: Erasing from middle of a list problem
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/e74fcbbad6312ec6?hl=en
==============================================================================

== 1 of 3 ==
Date: Sat, Nov 1 2008 3:52 am
From: Angus


If I want to erase all list items with a value of say 3 as below:

std::list<int> mylist;
mylist.push_back(3);
mylist.push_back(4);
mylist.push_back(5);
mylist.push_back(6);

for(std::list<int>::iterator it = mylist.begin(); it !=
mylist.end(); ++it) {
if(*it == 3) {
std::cout << "deleting 3" << std::endl;
mylist.erase(it);
}
}

I get an access violation in the loop iteration after an erase. What
is the recommended way to deal with this? ie iterate through a
container removing elements which meet a criterion? remove?

A

== 2 of 3 ==
Date: Sat, Nov 1 2008 4:02 am
From: fungus


On Nov 1, 11:52 am, Angus <anguscom...@gmail.com> wrote:
>
> I get an access violation in the loop iteration after an erase.

Yep, you just freed the memory referenced by "it".

> What
> is the recommended way to deal with this? ie iterate through a
> container removing elements which meet a criterion?  remove?
>

std::list<int>::iterator it = mylist.begin();
while (it != mylist.end()) {
if(*it == 3) {
std::cout << "deleting 3" << std::endl;
it = mylist.erase(it);
}
else {
++it;
}
}

== 3 of 3 ==
Date: Sat, Nov 1 2008 4:06 am
From: Marcel Müller


Angus schrieb:
> for(std::list<int>::iterator it = mylist.begin(); it !=
> mylist.end(); ++it) {
> if(*it == 3) {
> std::cout << "deleting 3" << std::endl;
> mylist.erase(it);
> }
> }
>
> I get an access violation in the loop iteration after an erase.

Of course, you just invalidated your iterator by erasing its element.

> What
> is the recommended way to deal with this?

erase returns an iterator to the next valid element.


> ie iterate through a
> container removing elements which meet a criterion? remove?

The algorithm remove_if will do the job for you.


Marcel


==============================================================================
TOPIC: Class templates and singleton container
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7a8d15d1f932b3df?hl=en
==============================================================================

== 1 of 1 ==
Date: Sat, Nov 1 2008 4:01 am
From: Bit Byter


I want to write a (singleton) container for instances of my class
templates, however, I am not too sure on how to:

1). Store the instances
2). How to write the acccesor method (instance()) to retrieve an
instance of particular template
3). What type to return an instance as ..

Assuming I have the following code:

class template

template <class T1, class T2>
class MyTree
{
T1 foo(const T2& a1);
T2 foobar(const T1& a1, const T2& a2);
};


// Notes
// Instance prototype not completed (see question 2 and 3)
// Ideally, when an instance of a particular class is requested, if it
dosen't yet exist, then an
// instance is created and stored in the 'repository'
class Container
{
instance();
};


//Main.cpp

int main(int argc, char* argv[])
{
Container c;

MyTree<double, int> * t1 = c.instance(/*some args here*/);
MyTree<string, double> *t2 = c.instance(/*some args here*/);

}


Last but not the least, I want to be able to treat objects returned by
the instance() method, in a generic way (i.e. in this example, I want
to be able to treat them generically, as trees). Should I use
inheritance (i.e. the class template inherits from a base Tree class)
like this:

template <class T1, class T2>
class MyTree : public Tree
{
T1 foo(const T2& a1);
T2 foobar(const T1& a1, const T2& a2);
};

or is there a better way?

==============================================================================

You received this message because you are subscribed to the Google Groups "comp.lang.c++"
group.

To post to this group, visit http://groups.google.com/group/comp.lang.c++?hl=en

To unsubscribe from this group, send email to comp.lang.c+++unsubscribe@googlegroups.com

To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.c++/subscribe?hl=en

To report abuse, send email explaining the problem to abuse@googlegroups.com

==============================================================================
Google Groups: http://groups.google.com/?hl=en

No comments: