Topic: Comparing Objects with Same Base Class


Author: "Arthur G. Martinez" <arthurgm@rice.edu>
Date: 1999/10/13
Raw View
Hi,
Is there a way to compare to whether a base class type is
a certain subclass type or another:

abstract class a{};

class b :: public a{};

class c :: public a{};

if an object was passed to a function of type a, how could you then
tell whether the object was actually of type b or type c.  I need to know
this because my classes b and have different methods, and I don't want to
call a method that doesn't exist, so I need to make sure beforehand what
subclass it belongs to.

Thanks a bunch.
art
arthurgm@rice.edu




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: LR <lruss@superlink.net>
Date: 1999/10/14
Raw View
Hi,
I'm not quite sure what you're trying to do.  As has been pointed out, I
think in another newsgroup, you could use dynamic_cast.  But I think
that turns into a difficult maintenance situation, if you have several
functions and you want to add a new derived class with yet another set
of functions.  Instead, you may want to try something like this:

-------------------------------------------------------
#include <iostream>
class Base {
public:
 virtual void f() const = 0;
 virtual void s() const {
  // this function just returns
 }
 virtual void t() const {
  // this function just returns
 }
 static void SomeFunc(const Base &b) {
  b.f();
  b.s();
  b.t();
 }
 virtual ~Base() {}
};
class D1 : public Base {
public:
 void t() const {
  std::cout << "D1::t()" << std::endl;
 }
 void f() const {
  std::cout << "D1::f()" << std::endl;
 }
};
class D2 : public Base {
public:
 void s() const {
  std::cout << "D2::s()" << std::endl;
 }
 void f() const {
  std::cout << "D2::f()" << std::endl;
 }
};
int main() {
 D1 d1;
 D2 d2;
 Base::SomeFunc(d1);
 Base::SomeFunc(d2);
 return 0;
}
---------------------------------------------

Or maybe that was obvious?

I'm not sure that this meets your condition for not calling functions if
they don't exist in the class, but it will be pretty much the same
thing.  Saves you the trouble of doing any number of dynamic casts, all
over your code, and testing them to see if they worked or not.  I think
that's part of the point of virtual functions.

But, as I said, I'm not sure I understand what you want to accomplish.

HTH.

LR.

Arthur G. Martinez wrote:
>
> Hi,
> Is there a way to compare to whether a base class type is
> a certain subclass type or another:
>[snip]
> if an object was passed to a function of type a, how could you then
> tell whether the object was actually of type b or type c.
> [snip] my classes b and have different methods, and I don't want to
> call a method that doesn't exist,
>[snip]


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Jason Nye" <jnye@nbnet.nb.ca>
Date: 1999/10/14
Raw View
void f(a & aRef)
{
    b* pB = 0;
    c* pC = 0;
    if (pB = dynamic_cast<b *>(&a) != 0)
    {
        // It's a b.
    }
    else if (pC = dynamic_cast<c *>(&a) != 0)
    {
        // It's a c.
    }
    else ...
}

The classes must have virtual functions and you must use rtti.

Cheers,
Jason

Arthur G. Martinez wrote in message <7u1uba$im7$1@joe.rice.edu>...
>
>Hi,
>Is there a way to compare to whether a base class type is
>a certain subclass type or another:
>
>abstract class a{};
>
>class b :: public a{};
>
>class c :: public a{};
>
>if an object was passed to a function of type a, how could you then
>tell whether the object was actually of type b or type c.  I need to know
>this because my classes b and have different methods, and I don't want to
>call a method that doesn't exist, so I need to make sure beforehand what
>subclass it belongs to.
>
>Thanks a bunch.
>art
>arthurgm@rice.edu
>
>
>
>
>[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
>[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
>[              --- Please see the FAQ before posting. ---               ]
>[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
>
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/10/14
Raw View
"Arthur G. Martinez" <arthurgm@rice.edu> writes:

>Is there a way to compare to whether a base class type is
>a certain subclass type or another:

>abstract class a{};

>class b :: public a{};

>class c :: public a{};

>if an object was passed to a function of type a, how could you then
>tell whether the object was actually of type b or type c.

If you want to know the exact type, you can compare the results
of typeid.
    if( typeid(this) == typeid(b*) { ... }
But I hope that isn't what you really want. Such code rapidly
becomes impossible to maintain.

More likely, you don't care whether the object type is exactly a b,
but whether it is "at least" a b -- possibly some type derived from b.
You can do that with a dynamic cast.  The hierarchy must be
polymorphic, meaning the base class has at least one virtual function.

void a::foo()
{
    // use f1 from 'a' hierarchy, g1 from 'b' hierarchy
    if( b* pb = dynamic_cast<b*>(this) )
 pb->f1();
    else if( c* pc = dynamic_cast<c*>(this) )
 pc->g1();
    else
 ... error
}

--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]