Friday, September 8, 2017

Digest for comp.lang.c++@googlegroups.com - 9 updates in 4 topics

Juha Nieminen <nospam@thanks.invalid>: Sep 08 06:43PM

I have quite a design dilemma, which I will introduce with a quiz
question: What does the following print?
 
//-------------------------------------------------------------
#include <iostream>
 
class Base
{
public:
Base { func(); }
 
virtual void func() const
{ std::cout << "base\n"; }
};
 
class Derived: public Base
{
public:
virtual void func() const override
{ std::cout << "derived\n"; }
};
 
int main() { Derived derived; }
//-------------------------------------------------------------
 
I understand why it does what it does. However, this poses a
problem.
 
I have a base class that implements certain functionality, and
is often instantiated directly. Many details of this functionality
can be specialized by derived classes, by implementing certain
virtual functions of the base class. These virtual functions specify
how the base class should behave in diverse situations. These
virtual functions may, for instance, return values which the base
class uses (such as colors, textures and other such stuff).
 
In some cases the base class needs to know these values in its
constructor, as well as elsewhere (during the existence of the
instance).
 
A very simplified (and simplistic) example of how it might work
(the actual situation is more complicated):
 
//-------------------------------------------------------------
class Base
{
public:
Base(some_parameters)
{
doStuffWithValues(getValues());
}
 
void anotherFunction(some_parameters)
{
doStuffWithValues(getValues());
}
 
private:
void doStuffWithValues(Values);
 
protected:
virtual Values getValues() const;
};
//-------------------------------------------------------------
 
 
You might immediately see the problem: It's getting the incorrect
values in its constructor, because it's getting its own values,
rather than the ones from the derived class.
 
I wonder if there's a nice and clean solution to this (which,
preferably, doesn't require changing the signature of the
base class constructor).
Bo Persson <bop@gmb.dk>: Sep 08 08:54PM +0200

On 2017-09-08 20:43, Juha Nieminen wrote:
 
> I wonder if there's a nice and clean solution to this (which,
> preferably, doesn't require changing the signature of the
> base class constructor).
 
The nice and clean solution IS to pass the values needed as parameters
to the constructor.
 
The base class might then even store the values it is passed and you
don't have to make getValues virtual.
 
 
 
Bo Persson
scott@slp53.sl.home (Scott Lurndal): Sep 08 06:54PM


>I wonder if there's a nice and clean solution to this (which,
>preferably, doesn't require changing the signature of the
>base class constructor).
 
 
One must ask _where_ does the derived class get the values
from? If from the derived constructor, then they could have been
passed to the base class constructor as well.
 
You might consider using an 'init' paradigm - where the base and
derived classes have init members instead of constructors, where
the derived init member explictly calls the base init member (and
the creator of either object calls the init function explicitly
after creating the object). As the object is fully realized by
the time the init member is called, the base class init function
can use the virtual functions without issue.
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Sep 08 11:39PM +0200

On 9/8/2017 8:43 PM, Juha Nieminen wrote:
 
> I wonder if there's a nice and clean solution to this (which,
> preferably, doesn't require changing the signature of the
> base class constructor).
 
There are many type safe solutions, and I once convinced Marshall to put
them in the FAQ,
<url:
https://isocpp.org/wiki/faq/strange-inheritance#calling-virtuals-from-ctor-idiom>
 
Also see this old blog posting of mine, which provides more holistic
concrete examples instead of just a listing of techniques,
<url:
https://alfps.wordpress.com/2010/05/12/how-to-avoid-post-construction-by-using-parts-factories/>
 
Bjarne discusses some aspects of why the 1990's two-phase construction
solution is bad, in <url: http://www.stroustrup.com/3rd_safe0.html>
(search the PDF for "init"), but he fails to address why it was so
commonly used, namely to solve your problem in a child's way, sort of.
 
Summing up:
 
• Don't use two-phase construction.
• Somehow (constructor args, templating) pass the requisite info to the
base class constructor.
 
 
Cheers & hth.,
 
- Alf
Jorgen Grahn <grahn+nntp@snipabacken.se>: Sep 08 05:39AM

On Thu, 2017-09-07, Öö Tiib wrote:
>> But with some trickery you can do almost anything.!!
 
> Do that trickery using https://groups.google.com/ like fir, (who brought
> it up) requested.
 
And do it on alt.test. Not here.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
fir <profesor.fir@gmail.com>: Sep 08 07:13AM -0700

W dniu czwartek, 7 września 2017 22:01:49 UTC+2 użytkownik Good Guy napisał:
 
> With over 500 million devices now
> running Windows 10, customer satisfaction is higher than any
> previous version of windows.
 
here it worked "of course it doesnt.." was in curier and "With over 500 milion " was yellow on blue
"Chris M. Thomasson" <invalid@invalid.invalid>: Sep 08 12:39PM -0700

On 9/8/2017 7:13 AM, fir wrote:
 
>> But with some trickery you can do almost anything.!!
 
>> I didn't see the original post but it looks like he/she might be on
>> my killer-switch!!
[...]
 
> here it worked "of course it doesnt.." was in curier and "With over 500 milion " was yellow on blue
 
Not for me. Well, I do view my messages in plaintext. ;^)
Lynn McGuire <lynnmcguire5@gmail.com>: Sep 08 12:01PM -0500

LLVM 5.0.0 released
http://lists.llvm.org/pipermail/llvm-announce/2017-September/000075.html
 
"This release is the result of the community's work over the past six
months, including: C++17 support, co-routines, improved optimizations,
new compiler warnings, many bug fixes, and more."
 
Hat tip to:
http://www.osnews.com/story/29998/LLVM_5_0_0_released
 
Lynn
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Sep 08 06:19AM -0700

A good follow-up statement / question and reply:
 
https://groups.google.com/d/msg/comp.lang.c/-dC-tMZeryM/t4lkvcU9AQAJ
 
Thank you,
Rick C. Hodgin
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: