Class hierarchy

N

Nikola

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
 
P

Pascal J. Bourguignon

Nikola said:
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 {
};
}
 
E

Erik Wikström

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 {
};
}
 
N

news.aioe.org

Nikola said:
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.
 
N

Nikola

Pascal said:
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
 
N

Nikola

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
 
E

Erik Wikström

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
{
};
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top