[prev in list] [next in list] [prev in thread] [next in thread] 

List:       lyx-devel
Subject:    Re: C++ question
From:       Angus Leeming <a.leeming () ic ! ac ! uk>
Date:       2002-10-31 11:10:31
[Download RAW message or body]

On Thursday 31 October 2002 10:57 am, Lars Gullik Bjřnnes wrote:
> Angus Leeming <a.leeming@ic.ac.uk> writes:
> | I read the recent thread about casting Insets up to
> | their "real" type and thought I'd try and implement André's
> | asAInset, asBInset, asCInset thing using templates.
> |
> | Unfortunately, it doesn't compile :-(
> |
> | Is their anyway to do this?
>
> Yes, by using my solution.
>
> I even posted it asked for testers...

Which requires that dynamic_cast works. Here it does.
I was trying instead to combine André's approach using 
virtual functions with your use of templates. Is that possible?

Anyway, to answer your call for testers, this is what happens here:

class Base {
public:
	virtual ~Base() {}

	template <typename T>
	T * d_up_cast() { return dynamic_cast<T*>(this); }
	template <typename T>
	T * s_up_cast() { return static_cast<T*>(this); }
};

class Derived1 : public Base {};

class Derived2 : public Base {};

#include <iostream>

int main ()
{
	Base * a = new Derived1;

	std::cout << a << std::endl;
	std::cout << "dynamic_cast: "
		  << a->d_up_cast<Derived1>() << ' '
		  << a->d_up_cast<Derived2>() << std::endl;
	std::cout << "static_cast: "
		  << a->s_up_cast<Derived1>() << ' '
		  << a->s_up_cast<Derived2>() << std::endl;
	return 0;
}

aleem@pneumon:aleem-> ./trial
140021f80
dynamic_cast: 140021f80 0
static_cast: 140021f80 140021f80
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic