PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > should this work?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
should this work?

Réponse
 
LinkBack Outils de la discussion
Vieux 08/04/2008, 22h35   #1
Puppet_Sock
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut should this work?

So I've had to take over a project that used an old compiler.
May not be able to step up to a newer compiler because the
project uses a library that has equivalent of the following in it.

#include <iostream>
class A
{
public:
A();
virtual void tryit(long val) = 0;
};

class B: public A
{
public:
virtual void tryit(long val);
long m_val;
};

A::A()
{
tryit(17);
}

void B::tryit(long val)
{
m_val = val;
}

int main()
{
B myB;
std::cout << myB.m_val << std::endl;
}

I don't think this should work in a compliant compiler. When class A
tries to find tryit, it should not. I get a linker error in my more
recent
compiler.

But the old compiler I'm using for the project happily compiles and
links, and behaves as though the class A is finding the entry in
class B for the function tryit.

Am I missing something? This shouldn't work should it?
Socks
  Réponse avec citation
Vieux 08/04/2008, 22h42   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: should this work?

Puppet_Sock wrote:
> So I've had to take over a project that used an old compiler.
> May not be able to step up to a newer compiler because the
> project uses a library that has equivalent of the following in it.
>
> #include <iostream>
> class A
> {
> public:
> A();
> virtual void tryit(long val) = 0;
> };
>
> class B: public A
> {
> public:
> virtual void tryit(long val);
> long m_val;
> };
>
> A::A()
> {
> tryit(17);


This is a call to a pure virtual function. The behaviour is U.

> }
>
> void B::tryit(long val)
> {
> m_val = val;
> }
>
> int main()
> {
> B myB;
> std::cout << myB.m_val << std::endl;
> }
>
> I don't think this should work in a compliant compiler. When class A
> tries to find tryit, it should not. I get a linker error in my more
> recent
> compiler.
>
> But the old compiler I'm using for the project happily compiles and
> links, and behaves as though the class A is finding the entry in
> class B for the function tryit.
>
> Am I missing something? This shouldn't work should it?


It should not.

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


  Réponse avec citation
Vieux 09/04/2008, 02h56   #3
Looney
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: should this work?

On Apr 9, 7:42 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> Puppet_Sock wrote:
> > So I've had to take over a project that used an old compiler.
> > May not be able to step up to a newer compiler because the
> > project uses a library that has equivalent of the following in it.

>
> > #include <iostream>
> > class A
> > {
> > public:
> > A();
> > virtual void tryit(long val) = 0;
> > };

>
> > class B: public A
> > {
> > public:
> > virtual void tryit(long val);
> > long m_val;
> > };

>
> > A::A()
> > {
> > tryit(17);

>
> This is a call to a pure virtual function. The behaviour is U.
>
>
>
> > }

>
> > void B::tryit(long val)
> > {
> > m_val = val;
> > }

>
> > int main()
> > {
> > B myB;
> > std::cout << myB.m_val << std::endl;
> > }

>
> > I don't think this should work in a compliant compiler. When class A
> > tries to find tryit, it should not. I get a linker error in my more
> > recent
> > compiler.

>
> > But the old compiler I'm using for the project happily compiles and
> > links, and behaves as though the class A is finding the entry in
> > class B for the function tryit.

>
> > Am I missing something? This shouldn't work should it?

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


It should not work as when A's constructor is executing it does not
have complete knowledge of it's VTable
as which functions have been overriden as the object construction has
not completed yet. So as a rule of thumb
never expect virtual calls inside constructor to call child class's
implementation.
  Réponse avec citation
Vieux 09/04/2008, 09h51   #4
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: should this work?

On Apr 8, 11:35 pm, Puppet_Sock <puppet_s...@hotmail.com> wrote:
> So I've had to take over a project that used an old compiler.
> May not be able to step up to a newer compiler because the
> project uses a library that has equivalent of the following in
> it.


> #include <iostream>
> class A
> {
> public:
> A();
> virtual void tryit(long val) = 0;
> };


> class B: public A
> {
> public:
> virtual void tryit(long val);
> long m_val;
> };


> A::A()
> {
> tryit(17);
> }


> void B::tryit(long val)
> {
> m_val = val;
> }


> int main()
> {
> B myB;
> std::cout << myB.m_val << std::endl;
> }


> I don't think this should work in a compliant compiler. When
> class A tries to find tryit, it should not. I get a linker
> error in my more recent compiler.


It's undefined behavior. With most compilers, you'll get a
runtime error, at least with optimization turned off.

> But the old compiler I'm using for the project happily
> compiles and links, and behaves as though the class A is
> finding the entry in class B for the function tryit.


> Am I missing something? This shouldn't work should it?


Since it is undefined behavior, anything which the compiler does
is "correct".

I'm curious, though. Suppose you drop the "= 0" on A::tryit,
and provide an implementation for the function. In that case,
the code is well defined, and a conforming compiler *must* call
A::tryit in the constructor. What does your old compiler do?
(And what compiler is it? This hasn't changed since the
earliest days of C++.)

--
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

  Réponse avec citation
Vieux 09/04/2008, 18h41   #5
Puppet_Sock
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: should this work?

On Apr 8, 5:35pm, Puppet_Sock <puppet_s...@hotmail.com> wrote:
[snip]
> Am I missing something? This shouldn't work should it?


Apologies. I was missing something. The code as I posted
it was not equivalent to the case I had. The function call
was being made from inside a virtual function of class A,
not from the ctor of A, and was always happening from an
instance of B.

#include <iostream>

class A{
public:
virtual void tryit(long val) = 0;
virtual void init(long val);
};

class B: public A
{
public:
virtual void tryit(long val);
long m_val;
};

void A::init(long val)
{
tryit(val);
}

void B::tryit(long val)
{
m_val = val;
}

int main()
{
B *pMyB;
pMyB = new B;
pMyB->init(19);
std::cout << pMyB->m_val << std::endl;
B otherB;
otherB.init(42);
std::cout << otherB.m_val << std::endl;
}


The output is:

19
42

Sigh. You think you understand the language, then you
read somebody else's code.
Socks
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h34.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11802 seconds with 13 queries