Global namespace vs. base class namespace

Global namespace vs. base class namespace

Post by Jeff Connell » Wed, 08 Nov 2000 04:00:00



Consider this code sample.

void f(){};
class A
{
public:
  virtual void f(){}

Quote:};

class B : public A
{
public:
  virtual void f(){ /* ??? */ }

Quote:};

int main()
{
 B b;
 b.f();
 return 0;

Quote:}

Replace ??? with f().  That is recursive as B::f is called.
Replace ??? with A::f().  A::f is called.
Replace ??? with ::f().  The global function f is called.

Let's assume that we actually want A::f.  At a later date, class C is
inserted in the hierarchy, i.e.
class A
class C : public A
class B : public C

Now we want B::f to call C::f, but we have to change the code.  What we
really wanted all along is for B::f to simply call "my base class
version", whichever that might be.  If ::f referred to "a namespace one
level up", it would work, but it really refers explicitly to the global
namespace (I think).  Is there a way *built in* to the language to call
a base class version of your function without explicitly naming the
base class?

--
Regards,
Jeff

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

 
 
 

Global namespace vs. base class namespace

Post by Victor Bazaro » Wed, 08 Nov 2000 04:00:00



> [...]  Is there a way *built in* to the language to call
> a base class version of your function without explicitly naming the
> base class?

No, and there cannot be.  What if B has two base classes?

class B : public A, protected AA, private AAA
{

Quote:};

Victor
--
Please remove capital A's from my address when replying by mail

 
 
 

Global namespace vs. base class namespace

Post by Jeff Connell » Wed, 08 Nov 2000 04:00:00





> > [...]  Is there a way *built in* to the language to call
> > a base class version of your function without explicitly naming the
> > base class?

> No, and there cannot be.  What if B has two base classes?

> class B : public A, protected AA, private AAA
> {
> };

Good point!  I was originally taught OO with Smalltalk, and I seldom
use multiple inheritance in C++.

--
Regards,
Jeff

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

 
 
 

Global namespace vs. base class namespace

Post by Jim Hyslo » Wed, 08 Nov 2000 04:00:00




Quote:> Consider this code sample.

> void f(){};
> class A
> {
> public:
>   virtual void f(){}
> };
> class B : public A
> {
> public:
>   virtual void f(){ /* ??? */ }
> };
[snip]
> Is there a way *built in* to the language to
> call
> a base class version of your function without explicitly naming the
> base class?

Not directly, but typedefs can be useful here:

class B : public A
{
   typedef A parent;
public:
   virtual void f() { parent::f(); }

Quote:};

It requires a manual change to the class if you insert a class between A
and B, but that's a single change of the typedef line, and if the
typedef is the first thing inside the class, it'll be easier to spot.

--
Jim
This message was posted using plain text only.  Any hyperlinks you may
see were added by other parties without my permission.
I do not endorse any products or services that may be hyperlinked to
this message.

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

 
 
 

1. Namespace prb: Calling base class functions sharing same relative class name

Hi all,

I have a class derived from two base classes which both use the same
class name, but come from different namespaces.

Now I would like a virtual function override in the derived class to
call both original implementations of that function from both base classes.

In the below code, I would like to see

space1::base::f
space2::base::f
derived::f

to be printed - but I was not able to accomplish this.

Does anyone have an idea what I might have done wrong?

I am using Visual C++ 6 SP5.

========cut=here=======
#include <iostream>
#include <cstdlib>

namespace space1 {
    struct base {
       virtual void f() {
          std::cout << "space1::base::f" << std::endl;
       }
    };

namespace space2 {
    struct base {
       virtual void f() {
          std::cout << "space2::base::f" << std::endl;
       }
    };

struct derived: space1::base, space2::base {
    virtual void f() {
       {
          using namespace space1;
          base::f();
       }
       {
          using namespace space2;
          base::f();
       }
       std::cout << "derived::f" << std::endl;
    }

int main(int, char **) {
    derived d;
    d.f();
    return EXIT_SUCCESS;
========cut=here=======

Thank you very much in advance for your help,

Guenther

Note: the 'From'-address shown in the header is an Anti-Spam
fake-address. Please remove the 'nospam'-prefix as well as the
'mapson'-substring from the address in order to get my real
email address.

PGP Key ID: 0x06410D41, 2048 bit RSA, created 2002/05/16
Fingerprint: 223A D51F ED62 33CE  208B 252C 8B92 555E
IDEA session keys preferred

2. Diect3D or OpenGL?

3. Enums in namespaces v/s classes in namespaces.

4. Sendmail and useless MX lookups while sending through UUCP.

5. Header files: std vs global namespace

6. FAQ: Books on programming 3D; reviews and more

7. namespace resolution and base classes

8. creatin combobox and text box in runtime in vb.net

9. nested classes in namespace scope and base-specifier-list scope

10. Application constants: static const class data vs namespaces

11. Shell Namespace : How to create a namespace like "My Computer"

12. Root shell namespace and Nonroot namespace.

13. Namespaces: IE4's Address Bar blank for my namespace.