Tuesday, February 2, 2010

comp.lang.c++ - 22 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:

* Avoiding pointers to member functions - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/91122aae5ea3693a?hl=en
* ❉♡❉free shippingwholesale cheap brand sunglass belts by palpal payment at
www.ecyaya.com - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/e0e6edca7e46b382?hl=en
* struct containing vector? - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/d0ba6cd9c3737991?hl=en
* Air Max90, 95,97,Air Max 360,Air Max LTD, Air Max 180,women Air Max 95,97,
Air max TN (www.vipchinatrade.com) - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/b53aaa1f80267ad0?hl=en
* scientific publications on the "Square-rectangle problem"? - 2 messages, 2
authors
http://groups.google.com/group/comp.lang.c++/t/17378f3e6e3294ba?hl=en
* Advice on using templates and vector to improve exception handling - 1
messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/36f052e700775e41?hl=en
* Cloning members of a hierarchy - 7 messages, 3 authors
http://groups.google.com/group/comp.lang.c++/t/1b4d4af934d8701a?hl=en
* 938460 Answers to your Computer question! 38 - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/75307d390c08046f?hl=en
* Safe to use substr? - 3 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/cece8bdad80abdbe?hl=en
* Poco VS Boost - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/f83daf3995fb7ee1?hl=en
* const char* to hex char - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/6e7413eddf65ca3a?hl=en

==============================================================================
TOPIC: Avoiding pointers to member functions
http://groups.google.com/group/comp.lang.c++/t/91122aae5ea3693a?hl=en
==============================================================================

== 1 of 2 ==
Date: Mon, Feb 1 2010 8:56 am
From: Chris Ahlstrom


James Kanze pulled this Usenet boner:

> On Jan 30, 7:00 pm, Robert Fendt <rob...@fendt.net> wrote:
>
>> I am trying to find the best way to make part of a class's
>> behaviour configurable during runtime, yet with as little
>> overhead as possible. Maybe someone here has an idea? So far I
>> have come up with 3 alternatives, all of which I do not really
>> like for different reasons.
>
>> (1) The 'simple' solution would be to implement various
>> alternatives of the (private) member function in question, and
>> use a "pointer to member function" to access it.
>
> But the real arguments against them are those you raised, along
> with the fact that you cannot associate data with them.

Is it worth using boost::bind to associate data with a function
pointer? And would also apply to function objects.

And for keeping the binding around, boost::function.

Disclaimer: I'm still early in my "exploration" of these Boost helpers.

--
He that is giddy thinks the world turns round.
-- William Shakespeare, "The Taming of the Shrew"


== 2 of 2 ==
Date: Mon, Feb 1 2010 10:55 am
From: Robert Fendt


And thus spake tonydee <tony_in_da_uk@yahoo.co.uk>
Mon, 1 Feb 2010 01:08:49 -0800 (PST):

> Also, for performance-sensitive image processing code, you're almost
> certainly much better off having a run-time switch on the outside of
> the loop, then reinstantiating a template for the specific loop/
> operations, gaining the benefits of full compile-time inlining and
> optimisation for each operation.

I am doing exactly that now. The current code uses a primitive form of traits (thus static polymorphism) and is completely templated, at least as far as the fixed infrastructure goes. The test application went from 300k to 3MB, but what the heck, I don't care. For the rest (a high-level transformation function queue), there will still have to be virtual dispatch, but that just can't be helped. The especially time-consuming stuff (oversampling, interpolation etc.) will be implemented completely in templates now (so it is effectively all inlined).

Robert


==============================================================================
TOPIC: ❉♡❉free shippingwholesale cheap brand sunglass belts by palpal payment
at www.ecyaya.com
http://groups.google.com/group/comp.lang.c++/t/e0e6edca7e46b382?hl=en
==============================================================================

== 1 of 1 ==
Date: Mon, Feb 1 2010 9:07 am
From: hero


❉♡❉free shippingwholesale cheap brand sunglass belts by palpal payment
at www.ecyaya.com

the latest hotsale Versace Sunglass offer in www.ecyaya.com

the latest hotsale Adidas Sunglass offer in www.ecyaya.com

the latest hotsale Armani Sunglass offer in www.ecyaya.com

the latest hotsale Burberry Sunglass offer in www.ecyaya.com

the latest hotsale Bvlgarl Sunglass offer in www.ecyaya.com

the latest hotsale Cartier Sunglass offer in www.ecyaya.com

the latest hotsale Chanel Sunglass offer in www.ecyaya.com

the latest hotsale Coach Sunglass offer in www.ecyaya.com

the latest hotsale Diesel Sunglass offer in www.ecyaya.com

the latest hotsale Dior Sunglass offer in www.ecyaya.com

the latest hotsale ED Sunglass offer in www.ecyaya.com

the latest hotsale Feidi Sunglass offer in www.ecyaya.com

the latest hotsale Gucci Sunglass offer in www.ecyaya.com

the latest hotsale LV Sunglass offer in www.ecyaya.com

the latest hotsale Nike Sunglass offer in www.ecyaya.com

the latest hotsale Okely Sunglass offer in www.ecyaya.com

the latest hotsale Police Sunglass offer in www.ecyaya.com

the latest hotsale Prada Sunglass offer in www.ecyaya.com

the latest hotsale Rayban Sunglass offer in www.ecyaya.com

the latest hotsale Roberto Cavali Sunglass offer in www.ecyaya.com

the latest hotsale Salvatore Sunglass offer in www.ecyaya.com

the latest hotsale Versace Sunglass offer in www.ecyaya.com


the latest hotsale Versace Sunglass offer in www.ecyaya.com

the latest hotsale Adidas Sunglass offer in www.ecyaya.com

the latest hotsale Armani Sunglass offer in www.ecyaya.com

the latest hotsale Burberry Sunglass offer in www.ecyaya.com

the latest hotsale Bvlgarl Sunglass offer in www.ecyaya.com

the latest hotsale Cartier Sunglass offer in www.ecyaya.com

the latest hotsale Chanel Sunglass offer in www.ecyaya.com

the latest hotsale Coach Sunglass offer in www.ecyaya.com

the latest hotsale Diesel Sunglass offer in www.ecyaya.com

the latest hotsale Dior Sunglass offer in www.ecyaya.com

the latest hotsale ED Sunglass offer in www.ecyaya.com

the latest hotsale Feidi Sunglass offer in www.ecyaya.com

the latest hotsale Gucci Sunglass offer in www.ecyaya.com

the latest hotsale LV Sunglass offer in www.ecyaya.com

the latest hotsale Nike Sunglass offer in www.ecyaya.com

the latest hotsale Okely Sunglass offer in www.ecyaya.com

the latest hotsale Police Sunglass offer in www.ecyaya.com

the latest hotsale Prada Sunglass offer in www.ecyaya.com

the latest hotsale Rayban Sunglass offer in www.ecyaya.com

the latest hotsale Roberto Cavali Sunglass offer in www.ecyaya.com

the latest hotsale Salvatore Sunglass offer in www.ecyaya.com

the latest hotsale Versace Sunglass offer in www.ecyaya.com

cool belts
the hottest Afflirtion Belt with high quality and low price on www.ecyaya.com

the hottest Armani Belt with high quality and low price on www.ecyaya.com

the hottest Bape Belt with high quality and low price on www.ecyaya.com

the hottest Boss Belt with high quality and low price on www.ecyaya.com

the hottest Burberry Belt with high quality and low price on www.ecyaya.com

the hottest C.D Belt with high quality and low price on www.ecyaya.com

the hottest Chanel Belt with high quality and low price on www.ecyaya.com

the hottest CK Belt with high quality and low price on www.ecyaya.com

the hottest D&G Belt with high quality and low price on www.ecyaya.com

the hottest Diesel Belt with high quality and low price on www.ecyaya.com

the hottest DSQ Belt with high quality and low price on www.ecyaya.com

the hottest ED Belt with high quality and low price on www.ecyaya.com

the hottest Gucci Belt with high quality and low price on www.ecyaya.com

the hottest Hermes Belt with high quality and low price on www.ecyaya.com

the hottest Levfs Belt with high quality and low price on www.ecyaya.com

the hottest LV Belt with high quality and low price on www.ecyaya.com

the hottest Polo Belt with high quality and low price on www.ecyaya.com

the hottest Prada Belt with high quality and low price on www.ecyaya.com

the hottest Versace Belt with high quality and low price on www.ecyaya.com

==============================================================================
TOPIC: struct containing vector?
http://groups.google.com/group/comp.lang.c++/t/d0ba6cd9c3737991?hl=en
==============================================================================

== 1 of 1 ==
Date: Mon, Feb 1 2010 9:31 am
From: cerr


On Feb 1, 8:45 am, cerr <ron.egg...@gmail.com> wrote:
> Hi There,
>
> I have a little issue going here:
> I have a xml configuration file that looks kinda like this:
> <PID>
>   <DESCRIPTION>Steveston Hwy</DESCRIPTION>
>   <BCAST>192.168.101.255</BCAST>
>   <DESTINATION>
>     <DESTNO>1531</DESTNO>
>     <DESTNO>301</DESTNO>
>     <DESTNO>12555</DESTNO>
>   </DESTINATION>
> </PID>
> Now there could be various <PID> tags as well as each <PID> would
> likely have multiple <DESTNO> tags.
> How do I store this best in my app?
> I thoughtr I'd declare a struct like this:
> struct structPID
> {
>         string strBcast;
>         string strDescription;
>         vector<int> Dest;};
>
> and just extend the array when there's more PIDs in that file.
> But when I read out the file i'm using my xml parser and i thought i'd
> do something like this:
> //i_PIDlist is the pointer to a variable decalred private in class
> PIDClient.
> bool PIDClient::readPIDConfig(string Configfile, vector<structPID>
> *i_PIDlist)
> {
>     RSXMLParser xmlPIDData(Configfile,
>                              RSXMLParser::FILE);
>
>     if (!xmlPIDData.IsDataGood()) {
>         OUTPUT(std::cerr << "Document " << conf->
>                          intersectionDataFile << " is not parsed
> successfully.\n";
>               )
>         return false;
>     }
>
>     string strPID = xmlPIDData.GetNodeData("PID");      //PID
>     while (strPID.size() > 0) {
>         RSXMLParser xmlPID(strPID);                     //PID details
>         structPID tmpPID;
>
>         tmpPID.strBcast=xmlPID.GetNodeData("BCAST");
>         tmpPID.strDescription=xmlPID.GetNodeData("DESCRIPTION");
>
>         string strDest = xmlPIDData.GetNodeData("DESTINATION");
>         while(strDest.size() > 0) {
>           RSXMLParser xmlPIDDest(strDest);
>           xmlPIDDest.GetNodeData("DESTNO");
>           tmpPID.Dest->push_back();
>         }
>         i_PIDlist->push_back(tmpPID);
>         //delete tmpPID;
>         //tmpPID=NULL;
>
>         strPID = xmlPIDData.GetNext();
>     }
>
>     return true;}
>
> But there's two problems i'm seeing just right out of the bat:
> If the outer while loop goes twice, how would tmpPID be re-declared?
> I thought about declaring it on the heap and then calling delete in
> the end of the funtion before it loops around but even then,
> I would do a i_PIDlist->push_back(tmpPID) and it woudl push on a
> pionter that gets ereased soon after...
>
> Thanks for a little help and guidance here.
>
> Ron

Ah,

I think I've figured out how I can do it:
How about this?

while (strPID.size() > 0) {
RSXMLParser xmlPID(strPID); //PID details
structPID *tmpPID = new structPID;

tmpPID->strBcast=xmlPID.GetNodeData("BCAST");
tmpPID->strDescription=xmlPID.GetNodeData("DESCRIPTION");

string strDest = xmlPIDData.GetNodeData("DESTINATION");
while(strDest.size() > 0) {
RSXMLParser xmlPIDDest(strDest);
tmpPID->Dest.push_back(atoi(xmlPIDDest.GetNodeData("DESTNO").c_str
()));
}
i_PIDlist->push_back((*tmpPID));
delete tmpPID;
tmpPID=NULL;

strPID = xmlPIDData.GetNext();
}

==============================================================================
TOPIC: Air Max90, 95,97,Air Max 360,Air Max LTD, Air Max 180,women Air Max 95,
97,Air max TN (www.vipchinatrade.com)
http://groups.google.com/group/comp.lang.c++/t/b53aaa1f80267ad0?hl=en
==============================================================================

== 1 of 1 ==
Date: Mon, Feb 1 2010 9:51 am
From: nice


Cheap Wholesale Shox NZ <www.vipchinatrade.com> paypal payment
Cheap Wholesale Shox OZ <www.vipchinatrade.com> free shipping
Cheap Wholesale Shox R2 <www.vipchinatrade.com> paypal payment
Cheap Wholesale Shox R3 <www.vipchinatrade.com> paypal payment
Cheap Wholesale Shox R3+R4 <www.vipchinatrade.com> paypal payment
Cheap Wholesale Shox R4 <www.vipchinatrade.com> paypal payment
Cheap Wholesale Shox R5 <www.vipchinatrade.com> free shipping
Cheap Wholesale Shox Reverie lover
Cheap Wholesale Shox RZ <www.vipchinatrade.com> paypal payment
Cheap Wholesale Shox TL <www.vipchinatrade.com> free shipping
Cheap Wholesale Shox Torch <www.vipchinatrade.com> paypal payment
Cheap Wholesale Shox TZ <www.vipchinatrade.com> paypal payment

Cheap Wholesale Nike Air Max 87 <www.vipchinatrade.com> free shipping
Cheap Wholesale Nike Air Max 89 <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Nike Air Max 90 <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Nike Air Max 91 <www.vipchinatrade.com> free shipping
Cheap Wholesale Nike Air Max 92 Man <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Nike Air Max 93 <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Nike Air Max 95 <www.vipchinatrade.com> free shipping
Cheap Wholesale Nike Air Max 97 <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Nike Air Max 180 Man <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Nike Air Max 2006 <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Nike Air Max 2009 <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Nike Air Max Clssic BW <www.vipchinatrade.com> free
shipping
Cheap Wholesale Nike Air Max LTD <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Nike Air Max Skyline <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Nike Air Max STAB <www.vipchinatrade.com> free
shipping
Cheap Wholesale Nike Air Max Tailwind <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Nike Air Max TN <www.vipchinatrade.com> paypal
payment

Air Force one <www.vipchinatrade.com> paypal payment
Cheap Wholesale Air Force One Man <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Air Force One Women <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Air Force One M&W <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Air Force one 25 Man <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Air Force One 25 Women <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Air Force One Kid <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Air Force one Mid Man <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Air Force one Mid Women <www.vipchinatrade.com> paypal
payment
Cheap Wholesale Air Force one Hight Women <www.vipchinatrade.com>
paypal payment

==============================================================================
TOPIC: scientific publications on the "Square-rectangle problem"?
http://groups.google.com/group/comp.lang.c++/t/17378f3e6e3294ba?hl=en
==============================================================================

== 1 of 2 ==
Date: Mon, Feb 1 2010 9:51 am
From: Kaz Kylheku


On 2010-02-01, Leslaw Bieniasz <nbbienia@cyf-kr.edu.pl> wrote:
>
>
> Hi,
>
> Are there any scientific publications about the "square-rectangle problem"
> (also known as the "circle-ellipse problem")

There is no problem.

The ``problem'' is whether you can model a circle as a subtype of
ellipse.

Doing this is only a problem if you allow mutability, without type
change.

Clearly, in mathematics, a circle is a kind of ellipse. But in
mathematics, we don't change an ellipse into a different ellipse while
lying to ourselves that it's still the same object.


== 2 of 2 ==
Date: Mon, Feb 1 2010 3:09 pm
From: Nilone


On Feb 1, 1:01 pm, Leslaw Bieniasz <nbbie...@cyf-kr.edu.pl> wrote:
> Hi,
>
> Are there any scientific publications about the "square-rectangle problem"
> (also known as the "circle-ellipse problem")
> and its possible treatments in C++?
> My search in literature databases does not reveal anything concrete.
> I would appreciate pointers to relevant publications, if there are any.
>
> Leslaw

Look for the following authors: Birkhoff, Cardelli, Cockburn, Date,
Guttag, Halpin, Kay, Liskov, Lispon, and Wing.

Alistair Cockburn has a good page on the topic, and some references
too:

http://alistair.cockburn.us/Constructive+deconstruction+of+subtyping

HTH

==============================================================================
TOPIC: Advice on using templates and vector to improve exception handling
http://groups.google.com/group/comp.lang.c++/t/36f052e700775e41?hl=en
==============================================================================

== 1 of 1 ==
Date: Mon, Feb 1 2010 10:01 am
From: jdm


On 30 Jan, 10:31, sebastian <sebastianga...@gmail.com> wrote:
>
> Hint: encapsulate everything in the "catch" block into a template
> function.

Thanks - good point!

But do you know of any way I can reduce the number of catch blocks
needed in the previous example?


==============================================================================
TOPIC: Cloning members of a hierarchy
http://groups.google.com/group/comp.lang.c++/t/1b4d4af934d8701a?hl=en
==============================================================================

== 1 of 7 ==
Date: Mon, Feb 1 2010 11:45 am
From: Paul N


I've got various classes derived (directly or indirectly) from a
single base, and want a function to make a copy of a given object. My
code is along the following lines:

class Base {
public:
int b;
virtual Base *clone() = 0;
};

class Derived : public Base {
public:
int d;
Base *clone() { return new Derived(*this); }
};

Am I right in thinking that I need to include a definition of the
"clone" function for every class that I want to be able to clone
objects of? And that this would be true even if I didn't make it a
pure virtual function? And that there is no easy way round this, short
of using macros to "tidy" the code up? But conversely, am I right in
thinking that the automatically-generated copy constructors will do
the actual donkey work of making a copy of all the members at all the
levels for me?

(I was also going to include a question about a potential bug in VC++,
in which it complained about not being able to instatiate an abstract
class - but it turned out that the problem was that one declaration
was declared "const" and the other wasn't, so it was my fault.)

Thanks.
Paul.


== 2 of 7 ==
Date: Mon, Feb 1 2010 12:08 pm
From: Victor Bazarov


Paul N wrote:
> I've got various classes derived (directly or indirectly) from a
> single base, and want a function to make a copy of a given object. My
> code is along the following lines:
>
> class Base {
> public:
> int b;
> virtual Base *clone() = 0;
> };
>
> class Derived : public Base {
> public:
> int d;
> Base *clone() { return new Derived(*this); }
> };
>
> Am I right in thinking that I need to include a definition of the
> "clone" function for every class that I want to be able to clone
> objects of?

Yes. In production code I often see a macro that does that. Don't
forget to write it in your class. Something like

#define DEFINE_CLONABLE(class_name) class_name* clone() \
{ return new class_name(*this); }

...
class Derived ...
DEFINE_CLONABLE(Derived)
};

> And that this would be true even if I didn't make it a
> pure virtual function?

If you didn't make it pure virtual, what would its body look like?

> And that there is no easy way round this, short
> of using macros to "tidy" the code up?

Not really. Unless I'm missing something obvious, of course.

> But conversely, am I right in
> thinking that the automatically-generated copy constructors will do
> the actual donkey work of making a copy of all the members at all the
> levels for me?

Seems so.

> (I was also going to include a question about a potential bug in VC++,
> in which it complained about not being able to instatiate an abstract
> class - but it turned out that the problem was that one declaration
> was declared "const" and the other wasn't, so it was my fault.)

That would certainly do it. :-)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


== 3 of 7 ==
Date: Mon, Feb 1 2010 12:17 pm
From: Robert Fendt


And thus spake Paul N <gw7rib@aol.com>
Mon, 1 Feb 2010 11:45:00 -0800 (PST):

> I've got various classes derived (directly or indirectly) from a
> single base, and want a function to make a copy of a given object. My
> code is along the following lines:
>
> class Base {
> public:
> int b;
> virtual Base *clone() = 0;
> };
>
> class Derived : public Base {
> public:
> int d;
> Base *clone() { return new Derived(*this); }
> };
>
> Am I right in thinking that I need to include a definition of the
> "clone" function for every class that I want to be able to clone
> objects of? And that this would be true even if I didn't make it a
> pure virtual function? And that there is no easy way round this, short
> of using macros to "tidy" the code up?

You can use a template. Without having it run through a compiler, something like this could/should work:

class Base
{
public:
virtual Base* clone() = 0;
};

template <typename T>
class ClonableBase : public Base
{
public:
virtual Base* clone()
{
return new T(*this);
}
}

class Derived: public ClonableBase<Derived>
{
...
};

This should work since every derived class inherits its own implementation of clone() from a template-generated 'buffer' class. However, I do not see much to be gained by this. Just implement the virtual constructors in the derived classes, that way at least the semantics become immediately clear.

> But conversely, am I right in
> thinking that the automatically-generated copy constructors will do
> the actual donkey work of making a copy of all the members at all the
> levels for me?

This works if your class looks more or less like a traditional C struct (i.e. a POD type, or 'plain old data'). However, be very, very careful if the constructor performs resource aquisition (and the destructor therefore frees resources). Then it will most probably NOT work, and can even lead to double-deletes and such pains.

As a rule, if your class contains at least one of the following, it will most probably need all three: destructor, copy constructor, assignment operator. In general do no depend on the generated versions, except in very simple cases; they have a tendency to not do what you want or need.

> (I was also going to include a question about a potential bug in VC++,
> in which it complained about not being able to instatiate an abstract
> class - but it turned out that the problem was that one declaration
> was declared "const" and the other wasn't, so it was my fault.)

Yes. C++ currently lacks an 'override' keyword (C++ 0x will get it, though), so you cannot tell the compiler that a function is supposed to override a function in the base class. This means that the compiler cannot give a meaningful error message, since it would have to 'guess' what yo actually want to achieve. That said, the Microsoft compiler _does_ support an override modifier as an extension. However, using non-standard extensions lead to non-portable code.

Robert

== 4 of 7 ==
Date: Mon, Feb 1 2010 1:13 pm
From: Paul N


On 1 Feb, 20:08, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Paul N wrote:
> > I've got various classes derived (directly or indirectly) from a
> > single base, and want a function to make a copy of a given object.

(snip)

> > Am I right in thinking that I need to include a definition of the
> > "clone" function for every class that I want to be able to clone
> > objects of?
>
> Yes.  In production code I often see a macro that does that.  Don't
> forget to write it in your class.  Something like
>
> #define DEFINE_CLONABLE(class_name) class_name* clone() \
>                                      { return new class_name(*this); }
>
> ...
>     class Derived ...
>       DEFINE_CLONABLE(Derived)
>     };

Interesting. I thought macros were generally disapproved of? I'll have
a think as to whether to do it this way.

>  > And that this would be true even if I didn't make it a
>
> > pure virtual function?
>
> If you didn't make it pure virtual, what would its body look like?

At one stage the body (in the base class, which was abstract due to
another pure virtual function) was { return NULL; } which satisfied
the compiler. A horrible kludge, though, and I'm glad it's gone.

(more snip)

Thanks for the help!
Paul.


== 5 of 7 ==
Date: Mon, Feb 1 2010 1:20 pm
From: Paul N


On 1 Feb, 20:17, Robert Fendt <rob...@fendt.net> wrote:
> And thus spake Paul N <gw7...@aol.com>
> Mon, 1 Feb 2010 11:45:00 -0800 (PST):
> > I've got various classes derived (directly or indirectly) from a
> > single base, and want a function to make a copy of a given object. My
> > code is along the following lines:
>
> > class Base {
> >   public:
> >   int b;
> >   virtual Base *clone() = 0;
> >   };
>
> > class Derived : public Base {
> >   public:
> >   int d;
> >   Base *clone() { return new Derived(*this); }
> >   };
>
> > Am I right in thinking that I need to include a definition of the
> > "clone" function for every class that I want to be able to clone
> > objects of? And that this would be true even if I didn't make it a
> > pure virtual function? And that there is no easy way round this, short
> > of using macros to "tidy" the code up?
>
> You can use a template. Without having it run through a compiler, something like this could/should work:
>
> class Base
> {
> public:
>   virtual Base* clone() = 0;
>
> };
>
> template <typename T>
> class ClonableBase : public Base
> {
> public:
>   virtual Base* clone()
>   {
>     return new T(*this);
>   }
>
> }
>
> class Derived: public ClonableBase<Derived>
> {
> ...
>
> };

So far I haven't learnt anyhting about templates, but this is further
evidence that it would be a good idea if I did.

> This should work since every derived class inherits its own implementation of clone() from a template-generated 'buffer' class. However, I do not see much to be gained by this. Just implement the virtual constructors in the derived classes, that way at least the semantics become immediately clear.
>
> > But conversely, am I right in
> > thinking that the automatically-generated copy constructors will do
> > the actual donkey work of making a copy of all the members at all the
> > levels for me?
>
> This works if your class looks more or less like a traditional C struct (i.e. a POD type, or 'plain old data'). However, be very, very careful if the constructor performs resource aquisition (and the destructor therefore frees resources). Then it will most probably NOT work, and can even lead to double-deletes and such pains.
>
> As a rule, if your class contains at least one of the following, it will most probably need all three: destructor, copy constructor, assignment operator. In general do no depend on the generated versions, except in very simple cases; they have a tendency to not do what you want or need.

Ah. I knew about the "rule of three". But you saying it makes me
realise that my class does in fact have a memory leak, one which I'd
totally overlooked before.

> > (I was also going to include a question about a potential bug in VC++,
> > in which it complained about not being able to instatiate an abstract
> > class - but it turned out that the problem was that one declaration
> > was declared "const" and the other wasn't, so it was my fault.)
>
> Yes. C++ currently lacks an 'override' keyword (C++ 0x will get it, though), so you cannot tell the compiler that a function is supposed to override a function in the base class. This means that the compiler cannot give a meaningful error message, since it would have to 'guess' what yo actually want to achieve. That said, the Microsoft compiler _does_ support an override modifier as an extension. However, using non-standard extensions lead to non-portable code.

Many thanks for your help.
Paul.


== 6 of 7 ==
Date: Mon, Feb 1 2010 2:06 pm
From: Robert Fendt


And thus spake Paul N <gw7rib@aol.com>
Mon, 1 Feb 2010 13:13:31 -0800 (PST):

> Interesting. I thought macros were generally disapproved of? I'll have
> a think as to whether to do it this way.

Disapproved in principle: no, not exactly. Disapproved when unnecessary: yes. Macros are dangerous, because they happen _before_ any 'real' language evaluation. The macro preprocessor is a glorified text replacement facility after all, nothing more. This does NOT mean that macros are without uses; in fact there are several scenarios where a macro is not only a possibility, but actually a superior one.

Problems arise when macros try to be "too clever". Since they are nigh unparsable by an IDE, they tend to lower readability and can (but not necessarily have to) make the code more difficult to maintain. As is the case with most things in C and C++: the art is to know when to use a language facility (and when not).

My personal rule is: avoid macros where sensible. Especially avoid meta-programming with macros, generating of classes and types, changing of identifier names and such. Many things can be achieved with templates, though not everything. And templates are a very complex additional meta-language, so they do not necessarily make things easier for you, especially in the beginning. They do respect types and namespaces, though, which is IMHO most important.

Robert

== 7 of 7 ==
Date: Mon, Feb 1 2010 2:46 pm
From: Robert Fendt


> So far I haven't learnt anyhting about templates, but this is further
> evidence that it would be a good idea if I did.

Templates are an integral part of modern C++, so you really should familiarise yourself with them. However, be aware that templates form a complete declarative meta-language. It is a lot like functional programming (just not on data but on types), and can get complicated quite quickly. Templates are difficult to read, and some of the compiler-errors produced by erroneous templates are more or less impossible to decipher. So start with simple cases. The most important thing to remember is that a C++ template is _not_ the same as generics in Java or C#. A template produces no code, unless it is 'instantiated' with the corresponding type or constant. Only than does the compiler produce code, not before.

The example I have shown is in fact not a 'simple' case, since the template receives the type of the inheriting class as its parameter (yes, that's legal). So each inheriting class is derived from its specific super class, which in turn inherits from the base class (and is generated automatically from the template). Stuff like that really can make the brain hurt if one is not used to it... at least it was like that for me.

Robert

==============================================================================
TOPIC: 938460 Answers to your Computer question! 38
http://groups.google.com/group/comp.lang.c++/t/75307d390c08046f?hl=en
==============================================================================

== 1 of 1 ==
Date: Mon, Feb 1 2010 12:07 pm
From: Andrew W.


I fell in love with the net and all the compuiter teahcnology in the very early years and have loved it ever since, I have an IT Degree and Communications degree and will be working on my doctorate for the next five years, in bwetween I design websites, databases, Multimedia Produxction or anything that wil make me a penny, just student scraping by. But Guess what? I don't want your money, your e-mail, or anything. Each of these sites has a specific topic but they have them style and layout sothey are easy to navigate and ther a search function should take you to exactly where youwant to, or just explore, they have outside service you can acccess as well. Each site has 10,000 plus articles and they are updated everyday so you know you are gettin up to date information. ENough, here the computer and tech related sites.

Code and Creation Application Level Programming and Development
http://codeandcreation.com

EyrLinx
http://eyelinx.com

EyeTraffif
http://eyetrafiic.net

FreeNetCommerce
http://freenetcommerce.com

FreeNew Windows Software
http://freenewwindowsofyware.com

Home DVD Production
http://homedvdproduction.com

Instant Web Solutions
http://instantwebsolutions.com

Ipods and Imacs
http://ipods-imacs.com

THe Flow of Traffic
http://theflowoftraffic

TotalHelpSolutionsa
http://totalhelpsolutions.com

Webtraffic Ultra
http://webtrafficultra

Thanks in Advance and if you find any of these sites hekofulto you please pass them along roytou friends.

ZGon>njJht#/d/w.kvzgWi=QKxbxXtPevWj

==============================================================================
TOPIC: Safe to use substr?
http://groups.google.com/group/comp.lang.c++/t/cece8bdad80abdbe?hl=en
==============================================================================

== 1 of 3 ==
Date: Mon, Feb 1 2010 12:37 pm
From: James Kanze


On Feb 1, 4:28 am, LR <lr...@superlink.net> wrote:
> James Kanze wrote:
> > On 31 Jan, 21:19, LR <lr...@superlink.net> wrote:
> >> Immortal Nephi wrote:

> > [...]
> >> I think this will work:

> >> const std::string
> >> data = std::string("Hello ") + '\0' + std::string("World");

> > An even simpler solution might be:
> > std::string const data( "Hello \0World", 12 );

> I didn't think of that, but I hate to count things since I
> think it makes maintenance more difficult.

Yes. I was afraid my usual solution would confuse the original
poster:

static char const init[] = "Hello \0World";
std::string const data(begin(init), end(init)-1);

(In this case, of course, begin and end are the usual template
functions.)

As soon as you accept to give a name to the initialization, you
can get the compiler to do the counting. You need the name,
however, since you need to refer to the initialization object
twice. (
static char const init[] = "Hello \0World";
std::string const data(init, init + sizeof(init) - 1);
will also work, but the begin and end solution is more general.)

--
James Kanze

value,


== 2 of 3 ==
Date: Mon, Feb 1 2010 12:38 pm
From: James Kanze


On Feb 1, 4:54 pm, James Lothian
<ja...@jamesNOSPAMlothian.freeserve.co.uk> wrote:
> LR wrote:
> > James Kanze wrote:
> >> On 31 Jan, 21:19, LR <lr...@superlink.net> wrote:
> >>> Immortal Nephi wrote:
> >> [...]
> >>> I think this will work:
> >>> const std::string
> >>> data = std::string("Hello ") + '\0' + std::string("World");
> >> An even simpler solution might be:
> >> std::string const data( "Hello \0World", 12 );

> > I didn't think of that, but I hate to count things since I think it
> > makes maintenance more difficult.

> Presumably then you could do something like:
> const char blah[] = "Hello \0World";
> const std::string data(blah, sizeof(blah));

sizeof(blah) - 1, if you don't want the final '\0'.

--
James Kanze


== 3 of 3 ==
Date: Mon, Feb 1 2010 12:42 pm
From: James Kanze


On Feb 1, 12:52 am, Öö Tiib <oot...@hot.ee> wrote:
> On Feb 1, 12:14 am, "Leigh Johnston" <le...@i42.co.uk> wrote:

> > >> If 0xFFFFFFFF or -1 is detected, then exception will be
> > >> thrown.

> > > (Again, -1 cannot be detected, because it cannot be
> > > represented on the type of the argument.)

> > assert(static_cast<unsigned int>(-1) == -1);

> > :)

> Anyway you get diagnostic warnings for it from most compilers.
> If 'static_cast<unsigned int>(-1)' is needed then '~0U' is
> perhaps shortest form that makes all compilers happy with it.

Except that it doesn't work. There are only two portable
solutions to get the vaue yourself:
static_cast< size_t >( -1 )
or
std::numeric_limits< size_t >::max();
(Both are guaranteed to be equal.)

Of course, the best solution is just to use std::string::npos.
There's no reason for you to worry about anything else. (And
you don't care what the value really is.)

--
James Kanze

==============================================================================
TOPIC: Poco VS Boost
http://groups.google.com/group/comp.lang.c++/t/f83daf3995fb7ee1?hl=en
==============================================================================

== 1 of 1 ==
Date: Mon, Feb 1 2010 4:47 pm
From: "Alf P. Steinbach"


* Larry:
>
> I was wondering what were the main differences between POCO C++ Library
> and BOOST C++ Library. I have already downloaded the Boost library and I
> find a really good library although I still have a long way to go with
> it. Recently, I have come across the Poco library and I must admit I
> find it quite good too. I would like to use one of those libraries to
> deal mostly with threads and mutex. Yet, the Poco library offers a good
> and easy way to deal with sockets also! Having said that, I wonder if
> Poco can be use the same way as Boost. I mean just including the path in
> VC++ Express. Is it like that or do I need to do some compiling?

With Poco you need to do some compiling, yes.

Poco and Boost address different levels of programming. Boost gives you the kind
of general functionality that conceivably could be directly part of the standard
library, intended to help you build more directly practically useful
functionality. Poco is meant to give you the more directly practically useful
functionality, more like Java's standard library, at the cost of having more
dependencies between parts so that it's more of an "all or nothing" thing.

That said, I don't see what this has to do with Windows programming. Both
libraries are general, portable C++ libraries. You should have posted in a C++
group such as [comp.lang.c++], and I've cross-posted this reply there.


Cheers & hth.,

- Alf

==============================================================================
TOPIC: const char* to hex char
http://groups.google.com/group/comp.lang.c++/t/6e7413eddf65ca3a?hl=en
==============================================================================

== 1 of 2 ==
Date: Mon, Feb 1 2010 9:11 pm
From: Philliam Auriemma


Hey guys,

Say I have a const char* that equals "10". How would I go about
turning that into '\x10'? I've tried itoa and ssprintf or something,
and have been at it for an entire day but I can't figure it out.


== 2 of 2 ==
Date: Mon, Feb 1 2010 9:42 pm
From: Jerry Coffin


In article <904e9ffb-95dd-4324-b789-cf0fd0e1b5f8
@d27g2000yqn.googlegroups.com>, phil.auriemma@gmail.com says...
>
> Hey guys,
>
> Say I have a const char* that equals "10". How would I go about
> turning that into '\x10'? I've tried itoa and ssprintf or something,
> and have been at it for an entire day but I can't figure it out.

This will convert the string to an unsigned long, assuming the input
is in hexadecimal:

unsigned long value = strtoul("10", NULL, 16);

Printing that value back out in hexadecimal with a leading '0x' looks
something like:

std::cout << std::hex << std::showbase << value << "\n";

--
Later,
Jerry.


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

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: