Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. The Cayo Perico Heist
      2. Find Lobbies & Players
      3. Guides & Strategies
      4. Vehicles
      5. Content Creator
      6. Help & Support
    2. Red Dead Online

      1. Frontier Pursuits
      2. Find Lobbies & Outlaws
      3. Help & Support
    3. Crews

    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. Grand Theft Auto Series

      1. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Guides & Strategies
      2. Help & Support
    6. GTA Vice City

      1. Guides & Strategies
      2. Help & Support
    7. GTA III

      1. Guides & Strategies
      2. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

      1. GTANet 20th Anniversary
    2. Support

      1. Court House
    3. Suggestions

Weekly C++ Challenge #1 - Template Virtuals


K^2

Recommended Posts

I don't know if I'll have enough steam or gather enough interest to really keep this weekly, but lets try.

 

I'm going to try and keep all of these challenges moderately practical, compliant with C++ standards, and hopefully interesting. None of them will require extensive knowledge of algorithms or large amounts of code to solve. Just various non-trivial ways to use the language.

 

 

Without further distraction, lets jump into this week's challenge. Anyone who has worked with or studied C++ had to deal with virtual functions. They can be fantastic ways to distinguish derived classes. For example, I might have a base class Foo with a virtual method Foo::IsInt() that tells me if some derived class is specialized to deal with integer types. But what if derived class is templated? What if you want to be able to distinguish between many base types a function could be using? Wouldn't it be fantastic if we could just call Foo::IsType<int>() instead? Unfortunately, C++ forbids declaring template member function virtual. And yet, this is precisely what I want you to overcome in this challenge. Write implementations for base class Foo and template class Bar derived from it which support the following code.

 

 

#include <iostream>class Foo;template <typename T>class Bar;void CheckInt(Foo* x){	if (x->IsType<int>())		std::cout << "is int.\n";	else		std::cout << "is not int.\n";}int main(){	Bar<int> x;	Bar<float> y;	std::cout << "x ";	CheckInt(&x);	std::cout << "y ";	CheckInt(&y);	return 0;}
Desired output.

 

x is int.y is not int.
Small hint.

 

Template specialization is done at compile time. Virtual functions are selected at run time. If there was only a way to bridge that gap...

 

 

Bigger hint.

 

Static variables inside template functions get initialized for each template initialization and will persist through run time.

 

 

Solution.

 

While there are several ways, in theory, to get type information at run time, by far the simplest, and perhaps most elegant, is to simply have a template function that generates unique type identifiers.

 

 

class Foo{    public:	template <typename T>	bool IsType()	{		return IsTypeImpl(GetTypeId<T>());	}	virtual bool IsTypeImpl(size_t id) = 0;    protected:	static size_t GetNextTypeId()	{		static size_t id = 0;		return id++;	}	template <typename T>	size_t GetTypeId()	{		static size_t id = GetNextTypeId();		return id;	}};template <typename T>class Bar : public Foo{    public:	bool IsTypeImpl(size_t id)	{		return id == GetTypeId<T>();	}};

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • 2 Users Currently Viewing
    0 members, 0 Anonymous, 2 Guests

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.