Cline/Lomow Book (Original) FAQ # 158

T

Tom

FAQ 158 is about correct usage of OO. I am struggling to understand
how the correct function is selected. Three derived classes from base
class "Printer2". Where is the logic that selects from the three
over-rides? That small decoration "p" in the next to last line of code
seems to be the key. This rookie just doesn't 'get it'. Thanks for any
help.

Below is a copy of the FAQ 158 program:

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

class Printer2 {
public :
virtual ~Printer2( ) { }
virtual void italics(const char* s) = 0;
};

class EpsonPrinter2 : public Printer2 {
public :
virtual void intalics(const char* s)
{ cout << esc << "i+" << s << esc << "i-"; }
};

class ProprinterPrinter2 : public Printer2 {
public :
virtual void intalics(const char* s)
{ cout << esc << "[i" << s << esc << "[n"; }
};

class StarPrinter2 : public Printer2 {
public :
virtual void intalics(const char* s)
{ cout << esc << "x" << s << esc << "y"; }
};

void
printUsingItalics ( Printer2& p, const char* s)
{
p.italics(s);
}
 
N

Neelesh Bodas

Tom said:
FAQ 158 is about correct usage of OO. I am struggling to understand
how the correct function is selected. Three derived classes from base
class "Printer2". Where is the logic that selects from the three
over-rides? That small decoration "p" in the next to last line of code
seems to be the key. This rookie just doesn't 'get it'. Thanks for any
help.

Below is a copy of the FAQ 158 program:

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

class Printer2 {
public :
virtual ~Printer2( ) { }
virtual void italics(const char* s) = 0;
};

class EpsonPrinter2 : public Printer2 {
public :
virtual void intalics(const char* s)
{ cout << esc << "i+" << s << esc << "i-"; }
};

class ProprinterPrinter2 : public Printer2 {
public :
virtual void intalics(const char* s)
{ cout << esc << "[i" << s << esc << "[n"; }
};

class StarPrinter2 : public Printer2 {
public :
virtual void intalics(const char* s)
{ cout << esc << "x" << s << esc << "y"; }
};

void
printUsingItalics ( Printer2& p, const char* s)
{
p.italics(s);
}

Since italics is a virtual function, it is bound dynamically. In other
words, the exact version of italics is selected from the actual class
held by the reference p.
 
M

marcas

Tom said:
FAQ 158 is about correct usage of OO. I am struggling to understand
how the correct function is selected. Three derived classes from base
class "Printer2". Where is the logic that selects from the three
over-rides? That small decoration "p" in the next to last line of code
seems to be the key. This rookie just doesn't 'get it'. Thanks for any
help.

Hi,

http://www.parashift.com/c++-faq-lite/virtual-functions.html

has a good explanation [see 20.4]

regards marcas
 
T

Tom

FAQ 158 is about correct usage of OO. I am struggling to understand
how the correct function is selected. Three derived classes from base
class "Printer2". Where is the logic that selects from the three
over-rides? That small decoration "p" in the next to last line of code
seems to be the key. This rookie just doesn't 'get it'. Thanks for any
help.

Below is a copy of the FAQ 158 program:

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

class Printer2 {
public :
virtual ~Printer2( ) { }
virtual void italics(const char* s) = 0;
};

class EpsonPrinter2 : public Printer2 {
public :
virtual void intalics(const char* s)
{ cout << esc << "i+" << s << esc << "i-"; }
};

class ProprinterPrinter2 : public Printer2 {
public :
virtual void intalics(const char* s)
{ cout << esc << "[i" << s << esc << "[n"; }
};

class StarPrinter2 : public Printer2 {
public :
virtual void intalics(const char* s)
{ cout << esc << "x" << s << esc << "y"; }
};

void
printUsingItalics ( Printer2& p, const char* s)
{
p.italics(s);
}

Since italics is a virtual function, it is bound dynamically. In other
words, the exact version of italics is selected from the actual class
held by the reference p.

Thanks for the reply Neelesh. I am definately struggling in the
transition to OO. Some of the examples within "C++ FAQs" are far more
advanced than my current skill level. The examples I guess are not
complete blocks of code? In particular the main( ) is missing? Your
response made me reread chapter #23 on references and referents. (130
pages deeper into the book. I have read it cover-to-cover and will
have to repeatedly it seems.)

So to make the code complete ... one needs to add something along the
following lines?

main ( )
{
StarPrinter2 p; // Creating A Derived Object
// Of Type StarPrinter2
char textString[] = "I have a dream to learn OO.";
printUsingItalics( p , textString );
}
 
N

Neelesh Bodas

Tom said:
So to make the code complete ... one needs to add something along the
following lines?

main ( )
{
StarPrinter2 p; // Creating A Derived Object
// Of Type StarPrinter2
char textString[] = "I have a dream to learn OO.";
printUsingItalics( p , textString );
}

I am really not sure. IIRR, this involves a problem of "slicing". You
are actually passing a reference to derived class, but the function
prototype expects a reference to base class.

Better is to do this :

Printer2 *p = new StarPrinter2;
char textString[] = "I have a dream to learn OO.";
printUsingItalics( *p , textString );

But as I said, I am not sure about this slicing thing (I know that it
happens when you have pointers in place of references)
 
D

deane_gavin

Neelesh said:
Tom said:
So to make the code complete ... one needs to add something along the
following lines?

main ( )
{
StarPrinter2 p; // Creating A Derived Object
// Of Type StarPrinter2
char textString[] = "I have a dream to learn OO.";
printUsingItalics( p , textString );
}

I am really not sure. IIRR, this involves a problem of "slicing". You
are actually passing a reference to derived class, but the function
prototype expects a reference to base class.

That's not a problem. You don't get slicing when you pass by reference
like that. You get slicing when you pass a derived object by *value* to
a function expecting a base object. Have you ever thrown an exception
derived from std::exception then caught a std::exception&. It works.
Better is to do this :

Printer2 *p = new StarPrinter2;
char textString[] = "I have a dream to learn OO.";
printUsingItalics( *p , textString );

Dynamic allocation and pointers are not necessary to avoid slicing. So,
as ever, if you don't need to manually control the lifetime of the
object, don't use dynamic allocation.

Gavin Deane
 
M

mlimber

Neelesh said:
Tom said:
So to make the code complete ... one needs to add something along the
following lines?

main ( )
{
StarPrinter2 p; // Creating A Derived Object
// Of Type StarPrinter2
char textString[] = "I have a dream to learn OO.";
printUsingItalics( p , textString );
}

I am really not sure. IIRR, this involves a problem of "slicing". You
are actually passing a reference to derived class, but the function
prototype expects a reference to base class.

Incorrect. Passing an object by value might slice it, but passing by
pointer OR by reference will both produce correct polymorphic behavior
and without chance of slicing. See these FAQs:

http://www.parashift.com/c++-faq-lite/value-vs-ref-semantics.html#faq-31.1
http://www.parashift.com/c++-faq-lite/value-vs-ref-semantics.html#faq-31.8
Better is to do this :

Printer2 *p = new StarPrinter2;
char textString[] = "I have a dream to learn OO.";
printUsingItalics( *p , textString );

But as I said, I am not sure about this slicing thing (I know that it
happens when you have pointers in place of references)

Better at least to use std::auto_ptr (or boost::scoped_ptr,
boost::shared_ptr, etc.) if you're going to use new. The OP's code is
better in my opinion since it avoids dynamic allocation altogether when
it is unnecessary.

Cheers! --M
 
A

Artie Gold

What are you talking about?

Had you quoted the relevant portions of the originally supplied code,
this would all be clearer.
No, it's an attempt to instantiate an abstract base class -- which you
can't do.

char textString[] = "I have a dream to learn OO.";
printUsingItalics( p , textString );
}


I am really not sure. IIRR, this involves a problem of "slicing". You
are actually passing a reference to derived class, but the function
prototype expects a reference to base class.

Better is to do this :

Printer2 *p = new StarPrinter2;

See above.
char textString[] = "I have a dream to learn OO.";
printUsingItalics( *p , textString );

But as I said, I am not sure about this slicing thing (I know that it
happens when you have pointers in place of references)
Please have *some* idea what you're doing before posting.

HTH,
--ag
 
N

Neelesh Bodas

Artie said:
What are you talking about?

Had you quoted the relevant portions of the originally supplied code,
this would all be clearer.

My reply was w.r.t. the tom's post and I just quoted the "only relavent
portions from that". May be I quoted less, will take care in future.
No, it's an attempt to instantiate an abstract base class -- which you
can't do.

StartPrinter2 is a derived class, derived from the base class Printer2
which is abstract. In other words, I am _not_ instantiating ABC - I am
simply creating an object of a derived class.

I believe that the confusion is due to the fact that OP has mis-spelled
italics as intalics in all derived classes. But looking at the context,
it was clear to me that OP actually meant italics at all these places.
char textString[] = "I have a dream to learn OO.";
printUsingItalics( p , textString );
}


I am really not sure. IIRR, this involves a problem of "slicing". You
are actually passing a reference to derived class, but the function
prototype expects a reference to base class.

Better is to do this :

Printer2 *p = new StarPrinter2;

See above.

Same applies here. I am _not_ instantiating an ABC.
char textString[] = "I have a dream to learn OO.";
printUsingItalics( *p , textString );

But as I said, I am not sure about this slicing thing (I know that it
happens when you have pointers in place of references)
Please have *some* idea what you're doing before posting.

OK. Thanks for that. Will remember in future.
 
M

marcas

Artie said:
No, it's an attempt to instantiate an abstract base class -- which you
can't do.

Hi,

it is true, that Printer2 is an abstract class. He makes an instance
of StarPrinter2, which is a non abstract class due to replacemant of the
pure virtual function. I think, that it is ok to assume that he meant
"italics" instead of "intalics".

regards marcas
 
M

mlimber

Artie said:
What are you talking about?

The OP doesn' understand the code snippet because it isn't a complete
program. He's supplying what's lacking. (Or are you just noting that he
didn't quote the whole message?)
Had you quoted the relevant portions of the originally supplied code,
this would all be clearer.

No, it's an attempt to instantiate an abstract base class -- which you
can't do.
[snip]

Clearly, there was just a typo ("intalics" instead of "italics"), and
this, as stated, is an attempt to instantiate (or, "create") a derived
object. What's your objection, exactly?

Cheers! --M
 
N

Neelesh Bodas

mlimber said:
Incorrect. Passing an object by value might slice it, but passing by
pointer OR by reference will both produce correct polymorphic behavior
and without chance of slicing. See these FAQs:

Yes you are right. Aplogoies.
Thanks (also to deane) for pointing out. It was a silly thing on my
part to get confused in this point.
 
A

Artie Gold

mlimber said:
Artie said:
What are you talking about?


The OP doesn' understand the code snippet because it isn't a complete
program. He's supplying what's lacking. (Or are you just noting that he
didn't quote the whole message?)

Had you quoted the relevant portions of the originally supplied code,
this would all be clearer.



No, it's an attempt to instantiate an abstract base class -- which you
can't do.

[snip]

Clearly, there was just a typo ("intalics" instead of "italics"), and
this, as stated, is an attempt to instantiate (or, "create") a derived
object. What's your objection, exactly?

Cheers! --M

Point taken.
--ag
 
T

Tom

My many thanks to those who responded to my confusion about FAQ #158.
I have learned more following the discussion and from the online
references I was pointed towards. It was definitely an error on my
part misspelling the overrides in the derived classes. There's a lot
of very smart folks in this group. A bit intimidating for sure, but if
I can just draft along the education will be a thrill.

BTW - I adjusted my font size in Agent editor and maybe now I won't
need stronger reading glasses and might catch my spelling errors more
easily.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top