Jump to content

Create new public variable from method.


Kip
 Share

Recommended Posts

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 by Kip
Link to comment
Share on other sites

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(); "

Link to comment
Share on other sites

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

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 :D

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 by Kip
Link to comment
Share on other sites

The code I'm showing, is very, very, very correct. Try it :D

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

  • 1 month later...

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.

#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 by Kip
Link to comment
Share on other sites

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

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

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...