为什么我的类字段在构造函数中被销毁(Why my class field is destroyed in constructor)
在我的构造函数中,我初始化一个字段。 初始化后,该字段将被销毁。 这是我的代码和我的测试:
A.hpp
class A { private: T t; public: A(); ~A(); void add(string name, string num); };
A.cpp
A::A() { cout << "Creating A\n"; t = T(100); cout << "End\n"; } void A::add(string name, string num) { cout << "calling add in A\n"; t.add(name, num); }
T.hpp
class T { private: E * t; public: T(int size=100); ~T(); void add(std::string name, std::string num);
T.cpp
T::T(int size) : size(size) { t = new E[size]; } T::~T() { cout << "Destroying\n"; // delete[] t; // if I don't comment this I get Segfault when calling add method } void T::add(string name, string num){ E e = E(name, num); t[0] = e; }
main.cpp中
int main(int argc, char* argv[]) { A a; a.add("name", "num"); }
产量
Creating A Destroying End calling add in A Destroying
In my constructor I initialize a field. The field is destroyed just after the initialization. Here's my code and my test:
A.hpp
class A { private: T t; public: A(); ~A(); void add(string name, string num); };
A.cpp
A::A() { cout << "Creating A\n"; t = T(100); cout << "End\n"; } void A::add(string name, string num) { cout << "calling add in A\n"; t.add(name, num); }
T.hpp
class T { private: E * t; public: T(int size=100); ~T(); void add(std::string name, std::string num);
T.cpp
T::T(int size) : size(size) { t = new E[size]; } T::~T() { cout << "Destroying\n"; // delete[] t; // if I don't comment this I get Segfault when calling add method } void T::add(string name, string num){ E e = E(name, num); t[0] = e; }
main.cpp
int main(int argc, char* argv[]) { A a; a.add("name", "num"); }
Output
Creating A Destroying End calling add in A Destroying
原文:
最满意答案
-pe
是两个参数的组合,-p
和-e
。-e
选项大致相当于Python的-c
选项,它允许您指定在命令行上运行的代码。 没有Python等效的-p
,它有效地添加代码以在从标准输入读取的循环中运行传递的代码。 为此,您实际上必须在代码中编写相应的循环,以及从标准输入读取的代码。
-pe
is a combination of two arguments,-p
and-e
. The-e
option is roughly equivalent to Python's-c
option, which lets you specify code to run on the command line. There is no Python equivalent of-p
, which effectively adds code to run your passed code in a loop that reads from standard input. To get that, you'll actually have to write the corresponding loop in your code, along with the code that reads from standard input.
相关问答
更多-
是的,谷歌很难找出标点符号,不幸的是,Perl似乎主要由标点符号组成:-) 命令行开关全部在perlrun中详细说明 。 (可从命令行调用perldoc perlrun ) 逐个进入选项: -p: Places a printing loop around your command so that it acts on each line of standard input. Used mostly so Perl can beat the pants off awk in terms of ...
-
/$/匹配“字符串的结尾”。 这可能是 字符串的结尾(如/\z/ ), 或者在字符串结尾之前的换行符之前(如/(?=\n\z)/ )。 (另外, /$/m匹配“行尾”,这可能是 字符串的结尾, 或者就在换行符之前(比如/(?=\n)/ )。 )。 使用您的替换/$/\n/g ,正则表达式匹配两次:一次在换行符之前,然后再次在字符串结尾处: 第一场比赛在换行之前: "foo\n" # ^ match 换行符放在当前匹配结束之前: "foo\n\n" # ^ insert before 下一场比 ...
-
Perl在@marderh的评论中指出了Docopt。 此外,还有其他模块提供类似的功能,如Getopt :: Auto , Getopt :: Euclid , Getopt :: AsDocumented ... Perl has Docopt as pointed out in the comments by @marderh. In addition, there are other modules that provide similar functionality such as Getopt: ...
-
是否有类似于Perl的Archive :: Extract的Python包?(Is there a Python package similar to Perl's Archive::Extract?)[2021-09-02]
借助Mark Byers的回应 ,也许你可以将所有这些与mimetypes (也来自标准库)结合在一起。 您可以识别文件类型,并相应地处理它。 世界需要一个Python版本的Archive::Extract ,我们希望你写它! Riding off of Mark Byers' response, perhaps you can tie all of these together with mimetypes (also from the standard library). You could ident ... -
将python输出保存到shell变量,类似于perl -e(save python output to shell variable, similar to perl -e)[2022-02-20]
$ test=$(python -c "print 'hello'") $ echo $test hello (作为旁注...)如果要保留换行符,请在echo使用引号: $ test=$(python -c "for i in range(3): print 'hello'") $ echo $test hello hello hello $ echo "$test" hello hello hello 最后一个提示: Perl比Python更适合一个衬里。 我倾向于做这样的事情,而不是强迫python ... -
为什么我的PE文件无效?(Why is my PE file invalid?)[2022-04-07]
你肯定是从错误的一端解决这个问题。 破解十六进制并不会让你得到你想要的东西,PE文件结构太复杂了。 你需要两件事。 Matt Pietrek的开创性文章是理解结构的必要读物。 在了解至少75%的代码之前,不要开始使用代码。 您需要Windows SDK。 include / winnt.h文件包含PE格式中使用的结构的声明。 它从_IMAGE_DOS_HEADER开始,这是文件的第一个块。 编写代码以从其声明中创建结构,这是获得有效可执行文件的唯一方法。 PS:您的十六进制转储挂起了试图查看您的问题的任何人 ... -
如何将时间戳证书添加到Linux上已签名的PE文件?(How to add timestamp certificate to a signed PE file on Linux?)[2022-10-06]
自2015年3月起, osslsigncode中有一个补丁 ,允许您通过PKCS#11令牌上的密钥签署代码。 它还不是官方发布的一部分。 所以你必须自己构建它,但它对我来说就像魅力一样。 示例调用如下所示: osslsigncode sign -pkcs11engine /usr/lib/engines/engine_pkcs11.so -pkcs11module /usr/lib/libeTPkcs11.so -certs ~/mysigningcert.pem -key 0:42ff -in ~/f ... -
自己解决了。 我使用了pefile python模块,我在其中提取了文本部分,并使用PointerToRawData和VirtualSize来推断文本部分的位置。 然后我用dd将.text段提取到一个单独的文件中。 import pefile pe = pefile.PE('filepath') for section in pe.sections: if section.Name == '.text' print "%s %s" % (section.PointerToRawData),h ...
-
是的,你可以用Python argparse做到这一点。 从文档: https : //docs.python.org/3/library/argparse.html#action 'append' - 存储一个列表,并将每个参数值附加到列表中。 这对于允许多次指定选项很有用。 用法示例: >>> parser = argparse.ArgumentParser() >>> parser.add_argument('--foo', action='append') >>> parser.parse_args ...
-
-pe是两个参数的组合, -p和-e 。 -e选项大致相当于Python的-c选项,它允许您指定在命令行上运行的代码。 没有Python等效的-p ,它有效地添加代码以在从标准输入读取的循环中运行传递的代码。 为此,您实际上必须在代码中编写相应的循环,以及从标准输入读取的代码。 -pe is a combination of two arguments, -p and -e. The -e option is roughly equivalent to Python's -c option, which l ...