首页 \ 问答 \ 为什么我的类字段在构造函数中被销毁(Why my class field is destroyed in constructor)

为什么我的类字段在构造函数中被销毁(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

原文:
更新时间:2022-06-16 15:06

最满意答案

-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: ...
  • 借助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 ...
  • $ 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文件结构太复杂了。 你需要两件事。 Matt Pietrek的开创性文章是理解结构的必要读物。 在了解至少75%的代码之前,不要开始使用代码。 您需要Windows SDK。 include / winnt.h文件包含PE格式中使用的结构的声明。 它从_IMAGE_DOS_HEADER开始,这是文件的第一个块。 编写代码以从其声明中创建结构,这是获得有效可执行文件的唯一方法。 PS:您的十六进制转储挂起了试图查看您的问题的任何人 ...
  • 自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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)