Kip Posted March 28, 2009 Author Share Posted March 28, 2009 (edited) I've got another problem, and I don't want to create a new topic for it. class SomeClass { public: template <typename UNKNOWN> number ( UNKNOWN iClass) { // do something } UNKNOWN operator() () { // << // return an 'UNKNOWN' class } }; In the first function I get a value with datatype: UNKNOWN. How do I make the second function return a UNKNOWN class? Edited March 28, 2009 by Kip MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API. Link to comment Share on other sites More sharing options...
Valik Posted March 28, 2009 Share Posted March 28, 2009 How exactly did you figure out how to make the first function work without being able to make the second function work given that the same method is used for both? Link to comment Share on other sites More sharing options...
Kip Posted March 28, 2009 Author Share Posted March 28, 2009 How exactly did you figure out how to make the first function work without being able to make the second function work given that the same method is used for both?That's not true, the first function gets called when the class is created: " SomeClass Hello; "The second gets called when it's used as a function: " Hello(); " MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API. Link to comment Share on other sites More sharing options...
Valik Posted March 28, 2009 Share Posted March 28, 2009 That's not true, the first function gets called when the class is created: " SomeClass Hello; "Not with the code you show. The code you show is a function taking a template argument and not specifying a return value. In other words, what you show is invalid code that generates a warning at the very least. It is not a constructor and it is never called. The second gets called when it's used as a function: " Hello(); "No shit. But thanks for assuming I've never written a functor before. Also, I'm not really sure why you think calling the code has any bearing on getting it correctly defined. But whatever. Your code is retarded from top to bottom. Why are you trying to apply the template parameter on a per-function basis? Anyway, to answer your question, here's how it works. I'm sure it's not what you expect: class Stupid { public: template<typename T> T operator() () { return T(32); // Just to prove something is returned. } }; Stupid Kip; std::cout<<Kip.operator()<int>()<<std::endl; As far as I know the compiler isn't smart enough to figure out what you are trying to do with the syntax you are trying to use because what you are trying to do is rather stupid. Make the class a template, not the individual members. Link to comment Share on other sites More sharing options...
Kip Posted March 28, 2009 Author Share Posted March 28, 2009 (edited) Not with the code you show. The code you show is a function taking a template argument and not specifying a return value. In other words, what you show is invalid code that generates a warning at the very least. It is not a constructor and it is never called.The code I'm showing, is very, very, very correct. Try it little mistake: try this code: class SomeClass { public: template <typename UNKNOWN> SomeClass ( UNKNOWN iClass) { // do something } UNKNOWN operator() () { // << // return an 'UNKNOWN' class } }; That was my fault then. Edited March 28, 2009 by Kip MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API. Link to comment Share on other sites More sharing options...
Valik Posted March 28, 2009 Share Posted March 28, 2009 The code I'm showing, is very, very, very correct. Try it little mistake: try this code: class SomeClass { public: template <typename UNKNOWN> SomeClass ( UNKNOWN iClass) { // do something } UNKNOWN operator() () { // << // return an 'UNKNOWN' class } }; That was my fault then.You really really need to stop insulting my intelligence when you are the one asking for help. Trying to tell me how code works when I can certainly read for myself is just absurd, especially when you are wrong. Now, I've showed you how to implement your stupid idea and I've told you how to do it right. Now bugger off and implement something. Link to comment Share on other sites More sharing options...
Kip Posted March 28, 2009 Author Share Posted March 28, 2009 As rude as you are, you actually helped me. ( ) MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API. Link to comment Share on other sites More sharing options...
Kip Posted May 3, 2009 Author Share Posted May 3, 2009 (edited) Hmm, I don't see what's wrong with this code. The only thing that could go wrong is that it assigns a double to an int, but that's prevented with the If statement. expandcollapse popup#include <iostream> #include <typeinfo> typedef std::string string; class variant { // There're probably better ways to do this, but that's not the point. private: int Int; float Float; double Double; long double Long_Double; bool Bool; short Short; unsigned short Unsigned_Short; unsigned int Unsigned_Int; long Long; unsigned long Unsigned_Long; long long Long_Long; unsigned long long Unsigned_Long_Long; string String; public: template <class Type> variant(Type Val) { if (typeid(Type) == typeid(int)) { this->Int = Val; std::cout << typeid(Type).name() << std::endl; } } }; int main() { variant Int = 9; // Works... variant Str = 8.8; // Uhh, nope.. doesn't work while (1) { } return 0; } Output:C:\Documents and Settings\HP_Administrator\Bureaublad\eeerm.cpp: In constructor `variant::variant(Type) [with Type = double]': C:\Documents and Settings\HP_Administrator\Bureaublad\eeerm.cpp:49: instantiated from here C:\Documents and Settings\HP_Administrator\Bureaublad\eeerm.cpp:31: warning: converting to `int' from `double'It's only a warning, but it won't compile. Edited May 3, 2009 by Kip MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API. Link to comment Share on other sites More sharing options...
Richard Robertson Posted May 3, 2009 Share Posted May 3, 2009 What exactly is "typeid"? I've never seen that before. Link to comment Share on other sites More sharing options...
Kip Posted May 3, 2009 Author Share Posted May 3, 2009 http://en.wikipedia.org/wiki/Typeidhttp://www.cplusplus.com/doc/tutorial/typecasting/ (scroll down a little bit) MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API. Link to comment Share on other sites More sharing options...
Valik Posted May 4, 2009 Share Posted May 4, 2009 Your code is poorly designed. The warning is... your warning. You are mixing compile-time and run-time features. The warning comes about because you are doing a run-time check to prevent assignment of a double to an int but the compiler is the compiler so it does not know this. Pretty much everything is wrong with the code you show. You should almost never need to use RTTI. You're wasting tons of space per variant object. Writing a variant class is well outside your ability at the moment, come back in six months or more of experience with the language. Link to comment Share on other sites More sharing options...
Richard Robertson Posted May 4, 2009 Share Posted May 4, 2009 Well, honestly that's a poor way to do it anyways. A variant should not be a template. Link to comment Share on other sites More sharing options...
Kip Posted May 4, 2009 Author Share Posted May 4, 2009 I know this isn't the best and fastest way to do it, but I just want it work. MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API. Link to comment Share on other sites More sharing options...
Richard Robertson Posted May 4, 2009 Share Posted May 4, 2009 The best way to do it is to have separate constructors for each type you want to handle, and overload the = operator. Link to comment Share on other sites More sharing options...
Kip Posted May 4, 2009 Author Share Posted May 4, 2009 Ah, thanks. MailSpons: Fake SMTP server for safe email testing Dutch postcode & address API. Link to comment Share on other sites More sharing options...
Valik Posted May 4, 2009 Share Posted May 4, 2009 I know this isn't the best and fastest way to do it, but I just want it work.Kip, teaching yourself how to write bad code is probably the stupidest possible thing you can do. Either learn how to do it right or use somebody else's code who does know how to do it right. Teaching yourself bad habits is just going to make both your life more difficult and the life of the unfortunate people like us whom you ask about your code. If you are not capable of writing something yet (and you are not capable of writing a variant class yet) then you need to be using something like the boost variant class. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now