Re: Is there anything in C++ akin to Java's class Object?
From: Arijit (pal_ari_at_yahoo.co.in)
Date: 07/08/04
- Next message: lokb: "Re: how to pass smart pointes to function."
- Previous message: Phlip: "Re: how to pass smart pointes to function."
- In reply to: Blue Ocean: "Re: Is there anything in C++ akin to Java's class Object?"
- Next in thread: puppet_sock_at_hotmail.com: "Re: Is there anything in C++ akin to Java's class Object?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: lokb: "Re: how to pass smart pointes to function."
- Previous message: Phlip: "Re: how to pass smart pointes to function."
- In reply to: Blue Ocean: "Re: Is there anything in C++ akin to Java's class Object?"
- Next in thread: puppet_sock_at_hotmail.com: "Re: Is there anything in C++ akin to Java's class Object?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|