Class hierarchy

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nikola

    #1

    Class hierarchy

    Is it possible in C++ to create a class hierarchy by inheritance that
    spans across various namespaces?

    For example, say I want to create a class which would behave like object
    type in C#, so that all classes I have would be below it in inheritance
    hierarchy, but my classes are scattered throughout various namespaces.

    Say I have 2 classes in namespace A and 2 in namespace B, where
    namespace A and namespace B are not related, and now I want to create
    class C so that all classes in namespace A and namespace B inherit from C.

    Is that possible and how is it done?

    Thanks,
    Nikola
  • Pascal J. Bourguignon

    #2
    Re: Class hierarchy

    Nikola <enlorkREMOVE@T HISgmail.comwri tes:
    Is it possible in C++ to create a class hierarchy by inheritance that
    spans across various namespaces?
    Of course. The two concepts are orthogonal.

    For example, say I want to create a class which would behave like
    object type in C#, so that all classes I have would be below it in
    inheritance hierarchy, but my classes are scattered throughout various
    namespaces.
    >
    Say I have 2 classes in namespace A and 2 in namespace B, where
    namespace A and namespace B are not related, and now I want to create
    class C so that all classes in namespace A and namespace B inherit
    from C.
    >
    Is that possible and how is it done?
    namespace C {
    class C {
    };
    }

    namespace A {
    class A1:public C::C {
    };
    class A2:public C::C {
    };
    }

    namespace B {
    class B1:public C::C {
    };
    class B2:public C::C {
    };
    }


    What would be more interesting to ask, is whether it's possible to
    have an intermediate class in an inheritance path be in a different
    namespace:

    namespace A {
    class A{
    };
    }

    namespace B {
    class B:public A {
    };
    }

    namespace A {
    class C:public B {
    };
    }

    --
    __Pascal Bourguignon__

    Comment

    • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

      #3
      Re: Class hierarchy

      On 2008-08-22 15:45, Pascal J. Bourguignon wrote:
      What would be more interesting to ask, is whether it's possible to
      have an intermediate class in an inheritance path be in a different
      namespace:
      >
      namespace A {
      class A{
      };
      }
      >
      namespace B {
      class B:public A {
      };
      }
      >
      namespace A {
      class C:public B {
      };
      }

      What exactly is it that I'm missing? To me this is no different from any
      other case where you inherit from a class in another namespace:

      namespace A {
      class A{
      };
      }

      namespace B {
      class B:public A::A {
      };
      }

      namespace A {
      class C:public B::B {
      };
      }

      --
      Erik Wikström

      Comment

      • news.aioe.org

        #4
        Re: Class hierarchy

        Nikola wrote:
        Is it possible in C++ to create a class hierarchy by inheritance that
        spans across various namespaces?
        >
        For example, say I want to create a class which would behave like object
        type in C#, so that all classes I have would be below it in inheritance
        hierarchy, but my classes are scattered throughout various namespaces.
        >
        Say I have 2 classes in namespace A and 2 in namespace B, where
        namespace A and namespace B are not related, and now I want to create
        class C so that all classes in namespace A and namespace B inherit from C.
        >
        Is that possible and how is it done?
        >
        Thanks,
        Nikola
        Why even ask. They are just symbols in different namespaces.

        Comment

        • Nikola

          #5
          Re: Class hierarchy

          Pascal J. Bourguignon wrote:
          >
          namespace C {
          class C {
          };
          }
          >
          namespace A {
          class A1:public C::C {
          };
          class A2:public C::C {
          };
          }
          >
          namespace B {
          class B1:public C::C {
          };
          class B2:public C::C {
          };
          }
          Thanks. My attempt was a bit different:

          class C {
          };

          namespace A {
          class A1 : public ::C {
          };
          }

          namespace B {
          class B1 : public ::C { // 'class B1 : public C {' was also tried
          };
          }

          The compiler went berserk on this, reporting loads of crazy error messages.
          What would be more interesting to ask, is whether it's possible to
          have an intermediate class in an inheritance path be in a different
          namespace:
          >
          namespace A {
          class A{
          };
          }
          >
          namespace B {
          class B:public A {
          };
          }
          >
          namespace A {
          class C:public B {
          };
          }
          I wasn't asking this for the sake of theoretical discussion, but thanks.
          Good to know this is possible as well.

          Nikola

          Comment

          • Nikola

            #6
            Re: Class hierarchy

            Anyway, my next question was to be this: If the following is the case:

            namespace A {
            class A {
            };
            }

            namespace B {
            class B : public A::A {
            };
            }

            Is it possible to have a method in class A which returns a value of type
            B or *B? My best attempt was:

            namespace A {
            class A;
            }

            namespace B {
            class B : public A::A; // <- Line 6
            }

            namespace A {
            class A {
            public:
            B::B *GetB();
            };
            }

            namespace B {
            class B : public A::A {
            };
            }

            This resulted in the error:
            test2.cpp:6: error: expected `{' before ‘;’ token

            So I tried the following:

            namespace A {
            class A; // <- Line 2
            }

            namespace B {
            class B : public A::A { // <- Line 6
            };
            }

            namespace A {
            class A {
            public:
            B::B *GetB();
            };
            }


            Which resulted with the following:
            test2.cpp:6: error: invalid use of incomplete type ‘struct A::A’
            test2.cpp:2: error: forward declaration of ‘struct A::A’

            Is that to say that what I want is impossible, or is there something I
            don't know?

            Thanks,
            Nikola

            Comment

            • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

              #7
              Re: Class hierarchy

              On 2008-08-25 13:20, Nikola wrote:
              Anyway, my next question was to be this: If the following is the case:
              >
              namespace A {
              class A {
              };
              }
              >
              namespace B {
              class B : public A::A {
              };
              }
              >
              Is it possible to have a method in class A which returns a value of type
              B or *B? My best attempt was:
              You got close, but you missed the fact that for a forward declaration it
              is of no interest which class B derives from:

              namespace B
              {
              class B; // Forward declaration
              }

              namespace A
              {
              class A
              {
              public:
              B::B *GetB();
              };
              }

              namespace B
              {
              class B : public A::A
              {
              };
              }

              --
              Erik Wikström

              Comment

              Working...