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

Re: inheritance in C++

56 views
Skip to first unread message
Message has been deleted

Victor Bazarov

unread,
Apr 27, 2015, 11:57:42 AM4/27/15
to
On 4/27/2015 11:36 AM, Stefan Ram wrote:
> I have not yet learned a lot about inheritance in C++. So, I tried
> to make up a program to »test all cases that can possibly happen".
>
> You can see this program and its output below.
>[..]
> It seems that there are only four lines that are really remarkable, viz.,
>
> calling non-static via base pointer to derived
> base m
> calling virtual via base pointer to derived
> derived v
>
> That is, when you call a non-virtual method via a pointer of
> type »base« that points to an instance of the class
> »derived«, the method declared in the base class will still
> be called, while in the case of a virtual method, the method
> in the derived class will be called.
>
> Are there any other cases that I forgot?

You can include a call to a virtual member from within a non-virtual
member function as an additional test. Like

void mv() { /* after own output */ this->v(); }

It will likely give you the same result as calling 'v' from 'main', yet
it demonstrates the whole point of polymorphism: while 'this' is
statically typed, since it's a pointer, its utilization is polymorphic
in nature.

Another exercise I suggest is 'const' versus non-const member functions.

Good luck!

V
--
I do not respond to top-posted replies, please don't ask
Message has been deleted

Paavo Helde

unread,
Apr 27, 2015, 12:48:13 PM4/27/15
to
r...@zedat.fu-berlin.de (Stefan Ram) wrote in
news:inheritance-2...@ram.dialup.fu-berlin.de:

> I have not yet learned a lot about inheritance in C++. So, I tried
> to make up a program to »test all cases that can possibly happen".
> { ::derived d;
> ::std::cout << "calling static via derived instance\n"s;
> ::d.s(); std::cout << "calling non-static via derived
> ::instance\n"s; d.m(); std::cout << "calling virtual via
> ::derived instance\n"s; d.v(); }

You can also call virtual method non-virtually:

d.base::v();

Luca Risolia

unread,
Apr 27, 2015, 4:38:32 PM4/27/15
to
Il 27/04/2015 17:36, Stefan Ram ha scritto:

> using namespace ::std::literals;

What do you keep prepending std:: with all those pedantic ::?

Luca Risolia

unread,
Apr 27, 2015, 4:39:45 PM4/27/15
to
> What do you keep prepending std:: with all those pedantic ::?

*Why


Message has been deleted

Luca Risolia

unread,
Apr 27, 2015, 5:18:15 PM4/27/15
to
Il 27/04/2015 23:14, Stefan Ram ha scritto:
> If you already know that they are »pedantic«,
> why should I care to explain?

It was a rhetorical question... :)

Christopher Pisz

unread,
Apr 27, 2015, 7:11:42 PM4/27/15
to
What does the adjective mean? Google says, "Like a pendant"...
but it also says synonyms are meticulous, precise, and exact.

Do we mean he know he doesn't need to put :: before std, but does it
anyway because of some sort of OCD-ed-ness? Just because he knows the
std namespace is global?


--
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---

woodb...@gmail.com

unread,
Apr 27, 2015, 10:35:11 PM4/27/15
to
On Monday, April 27, 2015 at 6:11:42 PM UTC-5, Christopher Pisz wrote:
> On 4/27/2015 4:18 PM, Luca Risolia wrote:
> > Il 27/04/2015 23:14, Stefan Ram ha scritto:
> >> If you already know that they are »pedantic«,
> >> why should I care to explain?
> >
> > It was a rhetorical question... :)
> >
>
> What does the adjective mean? Google says, "Like a pendant"...
> but it also says synonyms are meticulous, precise, and exact.
>
> Do we mean he know he doesn't need to put :: before std, but does it
> anyway because of some sort of OCD-ed-ness? Just because he knows the
> std namespace is global?
>

He may wish to avoid a situation like this:

namespace ha {
namespace std {

template <class T>
class vector
{
int a;
};

}
}

using namespace ha;

void marshal ()
{
std::vector<int> n;
}

The author of code like that may be incompetent or malicious.
There are a lot of fakes these days.

Brian
Ebenezer Enterprises - Was Eisenhower the last decent President?
http://webEbenezer.net

Öö Tiib

unread,
Apr 28, 2015, 3:12:45 AM4/28/15
to
Someone who hides global name 'std' locally deserves every
nuisance that was achieved that way. It is pointless to protect
against. Loss of such customer is actually a victory.

Luca Risolia

unread,
Apr 28, 2015, 3:39:25 AM4/28/15
to
On 28/04/2015 04:35, woodb...@gmail.com wrote:
> namespace ha {
> namespace std {
>
> template <class T>
> class vector
> {
> int a;
> };
>
> }
> }
>
> using namespace ha;
>
> void marshal ()
> {
> std::vector<int> n;
> }
>
> The author of code like that may be incompetent or malicious.

The author of code like that would not go far enough. At least my g++
compiler gives an error:

main.cpp: In function ‘void marshal()’:
main.cpp:17:3: error: reference to ‘std’ is ambiguous
std::vector<int> n;


Ian Collins

unread,
Apr 28, 2015, 3:09:42 PM4/28/15
to
woodb...@gmail.com wrote:
>>
>
> He may wish to avoid a situation like this:
>
> namespace ha {
> namespace std {
>
> template <class T>
> class vector
> {
> int a;
> };
>
> }
> }
>
> using namespace ha;
>
> void marshal ()
> {
> std::vector<int> n;
> }

Which happens how often in code snippets posted to Usenet? Or how often
in any sane C++ project?

The colons aren't pedantic, they are total superfluous clutter.

> The author of code like that may be incompetent or malicious.
> There are a lot of fakes these days.

Fake standard headers on a developer's machine?

--
Ian Collins

Drew Lawson

unread,
Apr 28, 2015, 3:56:45 PM4/28/15
to
In article <cqa47b...@mid.individual.net>
Ian Collins <ian-...@hotmail.com> writes:
>woodb...@gmail.com wrote:

>> The author of code like that may be incompetent or malicious.
>> There are a lot of fakes these days.
>
>Fake standard headers on a developer's machine?

This is a common paranoid mindset these days.

In the last couple days a resident loon in another group claimed
(while losing an argument) that you can't trust Google search
results. You see, they are *really* an evil conspirator backing
$OtherSide of $PoliticalTopic.

This even though the topic wasn't very political. But still, you
know, They are out to perpetrate fraud everywhere possible -- even
in Brian's header files.

--
Drew Lawson | Though it's just a memory,
| some memories last forever
0 new messages