How can I create my own var type?

This is a discussion on How can I create my own var type? within the C++ Programming forums, part of the General Programming Boards category; How can I create my own var type?...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question How can I create my own var type?

    How can I create my own var type?

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Classes or struct's

    The tutorials has info on both.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    don't forget enumerations
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Okay, but how can I make one like this:
    Code:
    MyVarName MyVar = 980;
    Instead of like this:
    Code:
    MyVarName MyVar;
    MyVar.StructPart = 980;

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    how about a template (I'm not too enlightened on them, so others will have to answer questions about them)
    Code:
    template <class T>
    class MyVarName
    {
    public:
    	MyVarName() {}
    	MyVarName(const T& a) {data = a;}
    	void operator=(const T& a) {data = a;}
    
    
    private:
    	T data;
    
    };
    
    
    int main()
    {
    	MyVarName<int> MyVar1 = 123;
    	MyVarName<std::string> MyVar2 = "Hello";
    	return 0;
    }
    Last edited by Ancient Dragon; 11-03-2005 at 04:11 PM.

  6. #6
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Well, classes are more powerful, and you could have multiple types of data in them, you would overload the = operator. But if you just want to say take a predefined type such as double and call it what you want (for whatever reason) just typedef it.

    Code:
    #include <iostream>
    using namespace std;
    
    
    typedef int My_Int;
    typedef double My_Double;
    
    
    		
    int main ()
    {
    
    
    	My_Int var_name_int; //Declares a variable of type My_int (which is just int)
    	My_Double var_name_double; 
    	My_Int my_array[2];
    
    
    	var_name_int = 5;
    	cout << var_name_int << endl;
    
    	var_name_double = 5.55;
    	cout << var_name_double << endl;
    
    	my_array[0] = 55;
    	my_array[1] = 555;
    
    	cout << my_array[0] + my_array[1] << endl;
    
    
    
    
    
    
    
    
    system("PAUSE");
    return 0;
    }

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    With typedef you can't really create a new type, but you can create a new name for an existing type. The following is quite common:

    Code:
    typedef unsigned long ULONG;  // Define a "new" type called ULONG
    
    ULONG x = 3;  // Define a new variable x, of type ULONG
    ULONG y = 4;  // Define a new variable y, of type ULONG
    Last edited by DougDbug; 11-03-2005 at 04:58 PM.

  8. #8
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Thanks guys, thats what was looking for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 11:35 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Replies: 5
    Last Post: 03-08-2005, 07:45 PM
  5. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 01:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21