Topic: Problem with virtual & overloaded functions


Author: "Sebastian Moleski" <sebmol@gmx.net>
Date: 2000/04/17
Raw View
Steve Clamage <stephen.clamage@sun.com>:
> Ron Natalie wrote:
> >
> > Fei Li wrote:
> > >
> > > Alexei Betin <xlelik@writeme.com>
wrote in message
> > >
news:8bb6l4$4p5rb$1@fu-berlin.de...
> > > > Not sure what "not working"
means...
> > > >
> > > > Beware that there is no
overloading resolution across scopes.
That means
> > > > that even if you override only
one of your overloaded functions in
derived
> > > > class, this function will hide
all the base class functions of the same
> > > > name.
> > >
> > > I know this is true for
non-virtual functions. It is true
> > > for virtual functions too? Could
you please point me
> > > to some references on this?
Thanks.
> > >
> >
> > The hiding has no concept of virtual
versus non-virtual.   However
> > the overload selection is only done
in the class through which you
> > are accessing the function (the
static type of the pointer/reference).
> > It's after the overloaded is
resolved that it decides if there is
> > a virtual overrride to be called:
>
> Also note that an overriding virtual
function does in fact hide the
> base-class function it overrides, as
well as all entities with the same
> name in all base classes.
>
> As Ron says, overloading and
overriding are independent concepts, and
> there are no special scope or hiding
rules for virtual functions.

So the next question would be: why? What
is the rationale behind forbidding such
design and requiring use of the "using"
statement/directive (whatever the
appropriate naming is)?

--
Sebastian Moleski
----
The Borland C++ Builder bug lists:
http://www.crosswinds.net/~bcbbugs/



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 2000/04/18
Raw View
In article <8dd1gc$1ubn$1@news.nikoma.de>,
  "Sebastian Moleski" <sebmol@gmx.net> wrote:
> So the next question would be: why? What
> is the rationale behind forbidding such
> design and requiring use of the "using"
> statement/directive (whatever the
> appropriate naming is)?
To prevent sudden, unexpected changes in behaviour if you add an
overloaded function.  For example:

class base
{
public:
};

class derived : public base
{
public:
   void f(double);
};

int main()
{
   derived d;
   d.f(0);
}

The compiler will happily accept this program, and implicitly convert
the int to a double.  Without the rule in question, if you add a
function 'f(int)' to base, you would suddenly change how the derived
class behaves - leading to all sorts of debugging headaches.

--
Jim
I ignore all email from recruitment agencies.  Except that
I reserve the right to send rude, nasty replies to recruiters.
Please do not send me email with questions - post
here.


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Fei Li" <feiscreen@email.msn.com>
Date: 2000/03/24
Raw View
Alexei Betin <xlelik@writeme.com> wrote in message
news:8bb6l4$4p5rb$1@fu-berlin.de...
> Not sure what "not working" means...
>
> Beware that there is no overloading resolution across scopes. That means
> that even if you override only one of your overloaded functions in derived
> class, this function will hide all the base class functions of the same
> name.

I know this is true for non-virtual functions. It is true
for virtual functions too? Could you please point me
to some references on this? Thanks.

Fei




---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 2000/03/24
Raw View

Fei Li wrote:
>
> Alexei Betin <xlelik@writeme.com> wrote in message
> news:8bb6l4$4p5rb$1@fu-berlin.de...
> > Not sure what "not working" means...
> >
> > Beware that there is no overloading resolution across scopes. That means
> > that even if you override only one of your overloaded functions in derived
> > class, this function will hide all the base class functions of the same
> > name.
>
> I know this is true for non-virtual functions. It is true
> for virtual functions too? Could you please point me
> to some references on this? Thanks.
>

The hiding has no concept of virtual versus non-virtual.   However
the overload selection is only done in the class through which you
are accessing the function (the static type of the pointer/reference).
It's after the overloaded is resolved that it decides if there is
a virtual overrride to be called:

class B {
public:
 virtual void  x(int);
 virtual void  x(const char*);
};

class D : public B {
public:
 void x(char*);
};

B* bp = new D;

bp->x("FOOBAR"); // Fine, finds the name B::x, finds a const char* overload, then invokes virtual D::x(char*)

D d;
 d.x(5);  // ERROR: find D::x, no int overload for x, fails.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/03/25
Raw View
Ron Natalie wrote:
>
> Fei Li wrote:
> >
> > Alexei Betin <xlelik@writeme.com> wrote in message
> > news:8bb6l4$4p5rb$1@fu-berlin.de...
> > > Not sure what "not working" means...
> > >
> > > Beware that there is no overloading resolution across scopes. That means
> > > that even if you override only one of your overloaded functions in derived
> > > class, this function will hide all the base class functions of the same
> > > name.
> >
> > I know this is true for non-virtual functions. It is true
> > for virtual functions too? Could you please point me
> > to some references on this? Thanks.
> >
>
> The hiding has no concept of virtual versus non-virtual.   However
> the overload selection is only done in the class through which you
> are accessing the function (the static type of the pointer/reference).
> It's after the overloaded is resolved that it decides if there is
> a virtual overrride to be called:

Also note that an overriding virtual function does in fact hide the
base-class function it overrides, as well as all entities with the same
name in all base classes.

As Ron says, overloading and overriding are independent concepts, and
there are no special scope or hiding rules for virtual functions.

--
Steve Clamage, stephen.clamage@sun.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Joerg Amhofer <joerg.amhofer@isagmbh.at>
Date: 2000/03/23
Raw View
Hello!

What I am trying to do is to implement base functionality in some base
class with virtual functions which are overloaded because I want to
use the same name for different types.

I want to derive other classes and override these functions there only
when needed (see example below).

But that is not working, why? The compiler gets confused about the
parameters.

Is this simply not possible with C++ or is it a compiler weakness
(VC6)?

Thanx in advance


The simpliest case I tried is this:

// Base functions. Overrideable when needed.
class A
{
    public:
    A() {}
    virtual void Action(string s)
    { cout << "A::Action(string)"; }
    virtual void Action(char* p, short l)
    { cout << "A::Action(char*,short)"; }
};

// needs new Action(char*, short)
class B : public A
{
    public:
    B() {}
    void Action(char* p, short l)
    { cout<<"B::Action(char*,short)"; }
};

// needs new Action(string)
class C : public A
{
    public:
    C() {}
    void Action(string s)
    { cout<<"C::Action(string)"; }
};

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 2000/03/23
Raw View

Joerg Amhofer wrote:
>
>
> But that is not working, why? The compiler gets confused about the
> parameters.
>

What do you mean "gets confused?"

It looks from your example that the following would be true:

 A* c = new C;
 A* b = new B;

 char* cp = ...some string...
 int len = strlen(foo);

 string str;


 b->Action(str);  // Calls A::Action(string)
 b->Action(cp, len); // Calls B::Action(char*, short)

 c->Action(str);  // Calls C::Action(string)
 c->Action(cp, len); // Calls A::Action(char*, short)

It's a questionable design though.  WHy is it not the case that you can't get them
to agree (or convert between) the two types?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Andreas Kahari <andkaha@my-deja.com>
Date: 2000/03/23
Raw View
In article <+MbYOJ+hDRrEptPwtUfpBikDv7=O@4ax.com>,
Joerg Amhofer <joerg.amhofer@isagmbh.at> wrote:
> Hello!
>
> What I am trying to do is to implement base functionality in some base
> class with virtual functions which are overloaded because I want to
> use the same name for different types.
>
> I want to derive other classes and override these functions there only
> when needed (see example below).
>
> But that is not working, why? The compiler gets confused about the
> parameters.
>
> Is this simply not possible with C++ or is it a compiler weakness
> (VC6)?
>
> Thanx in advance
>
> The simpliest case I tried is this:
>
> // Base functions. Overrideable when needed.
> class A
> {
> public:
> A() {}
> virtual void Action(string s)
> { cout << "A::Action(string)"; }
> virtual void Action(char* p, short l)
> { cout << "A::Action(char*,short)"; }
> };
>
> // needs new Action(char*, short)
> class B : public A
> {
> public:
> B() {}
> void Action(char* p, short l)
> { cout<<"B::Action(char*,short)"; }
> };
>
> // needs new Action(string)
> class C : public A
> {
> public:
> C() {}
> void Action(string s)
> { cout<<"C::Action(string)"; }
> };

My eyes are sore but my compiler is happy about your code. What exact
error messages do you get? What does you 'main' look like?

/A



--
# Andreas K   h   ri
# Brought to you from Uppsala, Sweden
# http://hello.to/andkaha
# Remember, an e-mail is as secure as a postcard


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Alexei Betin" <xlelik@writeme.com>
Date: 2000/03/23
Raw View
Not sure what "not working" means...

Beware that there is no overloading resolution across scopes. That means
that even if you override only one of your overloaded functions in derived
class, this function will hide all the base class functions of the same
name.

You can bring the base class functions into scope by using 'using'
directive - then the compiler will resolve your calls to overloaded
functions

>Hello!
>
>What I am trying to do is to implement base functionality in some base
>class with virtual functions which are overloaded because I want to
>use the same name for different types.
>
>I want to derive other classes and override these functions there only
>when needed (see example below).
>
>But that is not working, why? The compiler gets confused about the
>parameters.
>
>Is this simply not possible with C++ or is it a compiler weakness
>(VC6)?
>
>Thanx in advance
>
>
>The simpliest case I tried is this:
>
>// Base functions. Overrideable when needed.
>class A
>{
>    public:
>    A() {}
>    virtual void Action(string s)
>    { cout << "A::Action(string)"; }
>    virtual void Action(char* p, short l)
>    { cout << "A::Action(char*,short)"; }
>};
>
>// needs new Action(char*, short)
>class B : public A
>{
>    public:
>    B() {}
>    void Action(char* p, short l)
>    { cout<<"B::Action(char*,short)"; }
>};
>
>// needs new Action(string)
>class C : public A
>{
>    public:
>    C() {}
>    void Action(string s)
>    { cout<<"C::Action(string)"; }
>};
>
>---
>[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
>[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
>[              --- Please see the FAQ before posting. ---               ]
>[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
>


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]