Jump to content
  • Advertisement
Sign in to follow this  
Concentrate

Returning an object

This topic is 3261 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Observe :
#include<iostream>

using std::cout;
using std::endl;
using std::ostream;

template<typename T>
ostream& print(T arg, ostream& ostrm = cout){
	ostrm << arg; 
	return ostrm;
}

int main()
{
	
	print("Test\n");
	
	//want to do this
	//print("Test").(" double "); obvious Error here.

	return 0;
}

I am not quite sure how to achieve this. I could create a class but I cant overload the dot operator; Any Ideas? Also another question : can you give me an example of why one would use a "template typedef" ?

Share this post


Link to post
Share on other sites
Advertisement
You can get close to what you want like so:

#include <iostream>

struct StrangeWrapper {
template <typename T>
StrangeWrapper & operator()(const T & data) { stream << data; return *this; }
StrangeWrapper(std::ostream & os) : stream(os) {}
std::ostream & stream;
};

template <typename T>
StrangeWrapper print(T arg, std::ostream & ostrm = std::cout) {
ostrm << arg;
return StrangeWrapper(ostrm);
}

int main() {
print("Test\n");
print("Test")(" double ");
}

Share this post


Link to post
Share on other sites
I should have realized that since the dot operator can't be overloaded, there will
be no obvious legal way. Thanks for the input.

I guess I can overload different operator if needed.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!