Results 1 to 4 of 4

Thread: Attempting My String Class

  1. #1

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024

    Attempting My String Class

    Ok I have the suggestion that Zaei and Cornedbee gave me to use an inherted template, and I have added one of my functions to it to test it out. This is what I have so far:

    Code:
    template<typename C, typename T = std::char_traits<C>, typename A = std::allocator<C> >
    class TECHSTRING_CL : public std::basic_string<C, T, A>
    {
    public:
    	bool IsNumeric()
    	{
    		/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
    		const string	_DIGITS_C	= "1234567890.";		//Const String To Hold The Values Wanted
    		int				_iRet		= 0;					//Temp Return VAR
    		/*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
    		
    		if(this->length() <= 0)							//If A Bad Or Empty String Is Passed
    			return false;									//Return False
    
    		_iRet = this->find_first_not_of(_DIGITS_C);		//Make Sure Everything Is There
    		
    		return (_iRet == -1);								//If The String Is Numeric _iRet = -1
    	};
    };
    
    typedef TECHSTRING_CL<char> TechString;
    TechString s;
    But when I do the following:
    s = "123456";

    I get:
    error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const char [7]' (or there is no acceptable conversion)

    So whats up?
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    There are a few things that aren't properly inherited. Those are the constructors and the assignment operators (including combined assignment like +=), you have to rewrite these (by calling the parent of course).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Hmm, I have been trying to figure this out with no luck. Can you give me an example.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  4. #4
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    Here is an example:
    Code:
    class A
    {
    public:
    	A();
    	A(int);
    	A& operator = (int);
    	A& operator += (int);
    };
    
    class B : public A
    {
    };
    
    int main()
    {
    	B b1(1); // error, constructor not inherited
    	B b2;
    	b2 = 1;  // error, operator = not inherited
    	b2 += 1; // ok,    operator += IS inherited
    	return 0;
    }
    The solution to your problem would be to add constructors and assignment operators to your derived class, that just call the parents constructors / assignment operator:
    Code:
    typedef std::basic_string<C, T, A> string_type;
    inline TECHSTRING_CL& operator = (const char* str)
    {
    	string_type::operator = (str);
    	return *this;
    }
    Last edited by twanvl; Jun 12th, 2003 at 03:25 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width