LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-27-2011, 04:08 AM   #1
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
fun with templates, typedefs, and ,


Hello, everyone! I thought I'd share this bit of obscure C++ I came up with because it entertained me.
Code:
#include <iostream>


struct empty_struct
{
	typedef void print_type() const;
	virtual print_type printme = 0;
};


template <class Type>
void print_it(const Type &oObject, typename Type::print_type Type:: *mMembers[])
{ if (mMembers) while (*mMembers) (oObject.**mMembers++)(); }


class derived_struct : public empty_struct
{
public:
	print_type derived_struct:: *operator , (bool rReally) const
	{ return rReally? &derived_struct::showme : NULL; }

private:
	void showme() const
	{ std::cout << "showed!\n"; }

	void printme() const
	{ std::cout << "printed!\n"; }
};


int main()
{
	derived_struct object;

	empty_struct::print_type derived_struct:: *members[] =
	  { &empty_struct::printme, (object, true), (object, false) };

	print_it(object, members);
}
I personally think it's silly that this can be done, but I also appreciate that it can be done in principle. If only I could figure out how to define printme and showme using the typedef!
Kevin Barry
 
  


Reply

Tags
c++


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++: Typedefs and Exceptions CoderMan Programming 3 02-05-2010 05:30 PM
<fun> The Windows Crash </fun> Simon Bridge General 6 08-26-2007 07:46 PM
C: global typedefs dominik81 Programming 1 11-30-2003 02:34 PM
Templates MGLindsey Linux - Networking 0 07-02-2002 11:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration