Andre Poenitz <[EMAIL PROTECTED]> writes:

>> John Levon <[EMAIL PROTECTED]> writes:
>>
>> | I much prefer Andre's way. Compare
>> |
>> |    InsetSomething * i = inset->asSomethingInset();
>> |    if (!i)
>> |            break;
>> |
>> | with
>> |
>> |    if (inset->lyxCode() == SOMETHING_CODE) {
>> |            InsetSomething * i = static_cast<InsetSomething*>(i);
>> |            ...
>> |    }
>> |
>> | Does anyone have a problem with this form of RTTI ?
>>
>> What about some:
>>
>>        class Inset {
>>        public:
>>                template <typename T>
>>                T * insetAsType() {
>>                        return static_cast<T*>(this);
>>                }
>>        };
>>
>> Which in the usual case would not be implemented with static_cast as
>> above, but rather with some dynamic_cast or explit type checking with
>> some rtti.
>
| But if it is implemented with rtti, we could use rtti properly, couldn't we?
| And  static_cast<> does not solve the problem.

agree static_cast is not a solution at all.

we want to disallow crosscast and only allow upcast.

               Inset
                /\
               /  \
              /    \
            AInset  BInset

            Inset * i = new BInset;

            Ainset * a = dynamic_cast<AInset*>(i);

            a should now be 0. But is that true it is further checking
            needed?

It seems that I am right... it works as wanted:

#include <iostream>

using namespace std;

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

class AInset : public Inset {};

class BInset : public Inset {};

int main() 
{
        Inset * i = new BInset;
        AInset * a = dynamic_cast<AInset*>(i);
        BInset * b = dynamic_cast<BInset*>(i);
        cout << a << endl << b << endl;
}

./upcast
0
0x8049e40


So.. then does this work as well:

     class Inset {
           ...
           template <typename T>
           T * asType() {
             return dynamic_cast<T>(this);
           }
     };


This seems to work:

#include <iostream>

using namespace std;

class Inset {
public:
        virtual ~Inset() {}
        template <typename T>
        T asType() {
                return dynamic_cast<T>(this);
        }
};

class AInset : public Inset {};

class BInset : public Inset {};

int main() 
{
        Inset * i = new BInset;
        AInset * a1 = dynamic_cast<AInset*>(i);
        BInset * b1 = dynamic_cast<BInset*>(i);
        AInset * a2 = i->asType<AInset*>();
        BInset * b2 = i->asType<BInset*>();
        cout << a1 << endl << b1 << endl;
        cout << a2 << endl << b2 << endl;
}

./upcast
0
0x8049f18
0
0x8049f18


_but_ I guess a lot of complier have problems with RTTI...

-- 
        Lgb

Reply via email to