Re: Is there anything in C++ akin to Java's class Object?

From: Arijit (pal_ari_at_yahoo.co.in)
Date: 07/08/04


Date: 8 Jul 2004 13:29:28 -0700

aguilar.james@gmail.com (Blue Ocean) wrote in message news:<af9b506a.0407071839.1d7a1abb@posting.google.com>...
> "John Harrison" <john_andronicus@hotmail.com> wrote in message news:<opsasbi8db212331@andronicus>...
> > On 7 Jul 2004 13:19:54 -0700, Blue Ocean <blueoceanz1@hotmail.com> wrote:
> >
> > [snip]
>
> Thanks for the help. I guess that what the other guy said about how
> static type-checking is superior in general is true. Thinking about
> it, I can't think of any case in which I would have put objects of two
> different types into a collection last semester when I was using Java,
> so static type checking would not have made a difference. And if the
> performance is better, all the more reason to use it.

 One important reason(I think I read this in Stroustrup's website,
I don't remember) is that C++, unlike Java, supports multiple
inheritance. So, while in Java, you either inherit from object
or from some other class(which in turn inherits from object),
in C++ you may have inherit from three classes, which inherit
from superclass, so you end up with three superclass bases. (It
will not happen if the superclass is automatically virtual) But
this is a just a minor point. The important point is that what
you are trying to achieve in the Stack example can be done with
multiple inheritance, without using any superclass. For example

class D
{
public:
  virtual ~D();
};

class Stack
{
public:
  void push(D*);
  D* pop();
  // etc
};

class MyClass : public D //, public (other classes,not possible in Java)
{ };

int main()
{
  Stack S;
  MyClass My;

  S.push(&My);
  MyClass *M = dynamic_cast<MyClass*>( S.pop() );

  return 0;
}

 As you see, no templates are needed. Any class that you want
to store in the Stack just has to inherit from D.

-Arijit



Relevant Pages

  • Re: extend quesion
    ... Translation functionality. ... makes sense to inherit from a different class based on a commandline ... #could use include module instead of inheritance of superclass ...
    (comp.lang.ruby)
  • Re: [ubuntu-hardened] Re: Collecting NX information
    ... >> stack, if gcc can prove it doesn't need executable stack, it gets marked ... > stack trampoline:) But it's defficient right now, doesn't inherit when ... trampoline at all is just a bypass of the non-exec stack... ... I think mixing all these into one big flag is a mistake. ...
    (Linux-Kernel)
  • inheriting and changing default properties
    ... I also do not want to inherit an object from the "superclass" and set ... I create a "subclass" object. ... Name = 'bla bla'; ...
    (comp.soft-sys.matlab)
  • Re: Selecting a different superclass
    ... inherit from PointSet or TraceablePointSet based on whether I'm ... Can I select a superclass ... conditionally like this in Python? ...
    (comp.lang.python)
  • Re: Is there anything in C++ akin to Javas class Object?
    ... > would get linker errors when compiling files which ... > use the Object code compiled with no superclass but ... One could always implement a class Object and have any class that doesn't ... inherit anything inherit from Object explicitly. ...
    (comp.lang.cpp)