On 9/04/2018 8:51 AM, Andrew Rowley wrote:
On 9/04/2018 4:09 AM, Charles Mills wrote:
#include <stdio.h>

#define __IBMCPP_TR1__ 1
#include <regex>

class myRegex
{
public:
     std::tr1::regex regexObject;
};

int main(int argc, char* argv[])
{
     printf("RegEx test 4/6/2018\n");
     std::tr1::regex::flag_type flags = std::tr1::regex::extended;     //
necessary for error
     myRegex *myRegex_p = new myRegex;
     myRegex_p->regexObject.assign("foo", flags);
     delete myRegex_p;
     return 0;
}
C++ isn't my first language and I'm not even sure I found the right documentation, but does assign create a new regex rather than modifying the existing one?

What is the scope of the new Regex? And templates can create different classes based on the parameters so the result of regexObject.assign may not even be the same type of object as when myRegex was instantiated.


There are two classes created for C++ regex's, basic_regex<char> and basic_regex<wchat_t>. One for 8-bit chars and one for unicode 16-bit chars. Assign replaces the existing regex with a new one. In Charles case he hasn't
created a regex in the constructor so assign will just create a new one.

The scope is until delete is called. This is not idiomatic C++ where the MyRegex object would be instantiated on the stack and the scope is whatever the outer {} braces are. Deterministic resource finalization is one of the strengths of C++ and I would take it over Java GC any day of the week. It's also unusual to see the use of the new operator in modern C++ where raw pointers are generally banned by coding standards. One would use make_unique or make_shared
or on z/OS which doesn't fully support C++11 shared_ptr<T>(new T).

I'm not sure if that is allowed or not - this sort of stuff is why I avoid C++, too complex for me :-)


Indeed! Complex it most certainly can be. Having said that if you're lucky enough to have a modern C++ compiler and keep it simple it's actually an easy language to learn and incredibly powerful.

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to lists...@listserv.ua.edu with the message: INFO IBM-MAIN

Reply via email to