Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C++ custom data types

  • 15-12-2005 12:32am
    #1
    Closed Accounts Posts: 703 ✭✭✭


    I've been messin' with c++ for far too long now without actually doing anything, so I've decided to attempt something I've been considering for ages - a C++ implementation of a bittorrent client (big task, I know..).

    So heres the problem - I want to create a custom data type, bint (bencoded int), its basically an integer converted to a string with "i" prefixed and "e" postfixed" (EG: int 5 = 5 whereas bint 5 = "i5e"). Thus far the only way I can think of is using a class - I tried using a struct but it just wont work the way I tried and I'm not entirely happy with using a class.


Comments

  • Registered Users, Registered Users 2 Posts: 6,414 ✭✭✭kdouglas


    afaik your only option is a struct or class


  • Registered Users, Registered Users 2 Posts: 173 ✭✭happydude13


    I'm not sure that what you have described makes sense...

    Do you want a datatype that thinks it's an int ?
    and a string ?

    so you can use it as follows

    bint t1 = 5;
    bint t2 = "i5e";

    and t1 == t2 ?

    eof


  • Closed Accounts Posts: 703 ✭✭✭SolarNexus


    I'm not sure that what you have described makes sense...

    Do you want a datatype that thinks it's an int ?
    and a string ?

    so you can use it as follows

    bint t1 = 5;
    bint t2 = "i5e";

    and t1 == t2 ?

    eof
    sort of, but not quite.

    I want a datatype which I can say something like mybint += 5 and still be valid, whilst also being able to get its bencoded string; think of it as a special string type that accepts arthimetic operators. I guess I'm just asking too much to get this simply, a class might be the best option.


    an example:

    bint b1 = 5;

    while( true )
    {
    b1++;
    cout << "b1: " << b1; // outputs a string similar to "b1: i123e"
    }


  • Registered Users, Registered Users 2 Posts: 1,481 ✭✭✭satchmo


    You'll want to use a class with operator overloading, that'll do it. If you create a constructor that takes a single int as a parameter (and isn't an explicit constructor), you should be able to do bint b1=5; and the compiler will fill in the rest. You'll also need to implement all the other operators... +, -, ++, --, etc.

    And for the stream output you can also overload the global stream operator like this:
    std::ostream& operator<<(std::ostream &str, const bint &b){
        str<<"i"<<b.intValue()<<"e";
        return str;
    }
    
    presuming your bint class has an int intValue(void) function.


  • Closed Accounts Posts: 703 ✭✭✭SolarNexus


    class bint
    {
    private:
    	// data members
    	int val;
    	string benc;
    
    	// computers
    	void compute()
    	{
    		// numbers with leading 0's are invalid , fix later //
    		//////////////////////////////////////////////////////
    
    		// data members
    		char buff[17];
    		string pre = "i" , post = "e";
    
    		// error check
    		if( val == -0 )
    			val = 0;
    
    		// doit
    		_itoa_s( val , buff , 10 );
    
    		// return
    		benc = ( pre + buff + post );
    	}
    
    public:
    
    	// func
    	bint(int);
    
    	// mutators
    	void setVal( int value )
    	{
    		val = value;
    		compute();
    	}
    	
    	// aggregators
    	int getVal()
    	{
    		return val;
    	}
    	string getBencode()
    	{
    		return benc;
    	}
    
    
    };
    bint::bint( int value )
    {
    	if( value == NULL )
    	{
    		val = 0;
    		benc = "i0e";
    	}
    	else
    	{
    		val = value;
    		compute();
    	}
    }
    

    ^^ thats what I finished off just after my previous post.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,082 ✭✭✭Tobias Greeshman


    if(val == -0)
       val = 0 ;
     
    // should this not be
    if ( val < 0 )
       val = 0 ;
    

    How can you get leading zero's?? val is an int not a string, leading zero's can't happen.

    You just need some operator overloaded functions for operations like ==, !=, ++, --, +, -, =


  • Closed Accounts Posts: 703 ✭✭✭SolarNexus


    silas wrote:
    if(val == -0)
       val = 0 ;
     
    // should this not be
    if ( val < 0 )
       val = 0 ;
    

    How can you get leading zero's?? val is an int not a string, leading zero's can't happen.

    You just need some operator overloaded functions for operations like ==, !=, ++, --, +, -, =
    the reason for if( val == -0 ) is that -0 is not permitted in bencoding (http://www.bittorrent.com/protocol.html), but -1234 etc. is.

    you can get leading 0's in an integer: 00234 is equal to 234


Advertisement