Thread: My own regex...............class?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

    My own regex...............class?

    i was thinking of designs, and i came across this one which led me to question, would this be an gross misuse of a class?

    i'm fairly certain that this won't compile, but just pretend it will for now

    Code:
    string operator=(const Regex &r)
    {
    	return r.return_string;
    }
    
    class Regex
    {
    	friend string operator=(const Regex &);
            
    	public:
    	//ONE public function
    	Regex(string character_string, string expression);
    	
    	protected:
    	string return_string;	      
    	//....a bunch of private funtions 
    }
    
    int main()
    {
    	string to_match = "a2bcd4";
    	string result = "";	
    
    	//pretend like there is no need to escape any \'s here
    	string expression = "[a-z][0-9]\L\w+\E\d"; 
    	
    	result = Regex(to_match, expression);	
    }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Location
    South Africa
    Posts
    35
    You can't return a value from out of a constructor. Thats effectively what you're trying to do in main, with the line

    Code:
    result = Regex(to_match, expression);
    Maybe the line you're looking for is
    Code:
      Regex r(result, to_match, expression)
    You can change the compiler to take three arguments, the first a pointer or reference. That would work, I think.
    I code.
    I think.
    Ergo, I think in code.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    but i'm not returning a value from the constructor -

    i'm returning it from operator=(const Regex &)


    however, in my "foo-bar testing" , i cannot seem to get :perator=() to be able to access my private class elements. i know i have done the same thing with cout and <<, but it's not quite working as i had thought.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Location
    South Africa
    Posts
    35
    I think I see, yes...

    You want the compiler to mangle your code (on the right-hand-side of result=Regex(...); ) into sth like this?

    Code:
    operator=(const Regex r(to_match, expression));
    I'm not sure if that can work. It would be quite neat if it did, though.

    Methinks you're going to have to find a syntax for C++ somehwere and use that to check if what you want to do is possible.
    I code.
    I think.
    Ergo, I think in code.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    22
    I think what you want to do is define the conversion from a Regex to a string, instead of overloading the assignment operator.

    It must be a member function of the Regex class with a signiture something like:

    operator string() const;

    and would look something like:
    Code:
    Regex::operator string() const
    {
       return return_string;
    }

  6. #6
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by Koedoe
    I think I see, yes...

    You want the compiler to mangle your code (on the right-hand-side of result=Regex(...); ) into sth like this?

    Code:
    operator=(const Regex r(to_match, expression));
    I'm not sure if that can work. It would be quite neat if it did, though.

    Methinks you're going to have to find a syntax for C++ somehwere and use that to check if what you want to do is possible.
    Sure it will work, basically it creates a temporary object on the right and then using the operater= assigns the member string to the string variable on left. Syntax-wise it's the same as this:

    Code:
    string result;
    result = string("This is a string");
    EDIT: I know what you are doing wrong, the global operator= overload requires 2 parameter, not one as in the member function overload. So it needs to be:
    Code:
    string operator=(string & result, const Regex &r)
    {
    	result = r.return_string;
    	return &result;
    }
    you could return r.return_string but since it's const you would have to return it to a const string otherwise you'd create a backdoor to a private member variable.
    Last edited by Darryl; 04-08-2005 at 10:06 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM