I am trying to study the pimpl technique for C++. After went through some articles online, I found there are two different ways for pimpl, one is class X { public: X(...parameters...) ~X() private: struct Impl; Impl* impl_; }; The other way is to use a raw void pointer, like class X { public: X(...parameters...) ~X() private: void * impl_; }; Then use stat
我正在研究C ++的pimpl技术。 在网上浏览了一些文章后,我发现pimpl有两种不同的方式,一种是 class X { public: X(...parameters...) ~X() private: struct Impl; Impl* impl_; }; 另一种方法是使用一个原始的void指针,就像 class X { public: X(...parameters...) ~X() private: void * impl_; }; 然后使用static_cast将void指针转换回原始类型。 比较这两种方式有哪些优缺点?
I'm new to C++ and a bit confused regarding auto_ptr. I have a class which inside has a static auto_ptr. static std::auto_ptr<MyCompany::CConnection> con = std::auto_ptr<MyCompany::CConnection> (util::getDBConnection() ); Util::getDBConnection() implementation : CConnection* util::getDBConnection(){ try { cout<< &MyCompany::GetFermatCo
我是C ++的新手,对auto_ptr有点困惑。 我有一个内部有一个静态auto_ptr的类。 static std::auto_ptr<MyCompany::CConnection> con = std::auto_ptr<MyCompany::CConnection> (util::getDBConnection() ); Util :: getDBConnection()实现: CConnection* util::getDBConnection(){ try { cout<< &MyCompany::GetFermatConnection();
What is the best practice when returning a smart pointer, for example a boost::shared_ptr? Should I by standard return the smart pointer, or the underlying raw pointer? I come from C# so I tend to always return smart pointers, because it feels right. Like this (skipping const-correctness for shorter code): class X { public: boost::shared_ptr<Y> getInternal() {return m_internal;} pr
返回智能指针时的最佳做法是什么,例如boost :: shared_ptr? 我应该通过标准的方式返回智能指针还是底层的原始指针? 我来自C#,所以我总是返回智能指针,因为它感觉很好。 像这样(跳过短代码的const正确性): class X { public: boost::shared_ptr<Y> getInternal() {return m_internal;} private: boost::shared_ptr<Y> m_internal; } 不过,我见过一些经验丰富的编码员返回原始指针,并将原始
I recently read that signed integer overflow in C and C++ causes undefined behavior: If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined. I am currently trying to understand the reason of the undefined behavior here. I thought undefined behavior occurs here because the integer
我最近读到C和C ++中的有符号整数溢出会导致未定义的行为: 如果在评估表达式时,结果不是数学定义的,或者不在其类型的可表示值范围内,则行为是未定义的。 我目前正试图了解这里未定义行为的原因。 我认为未定义的行为发生在这里,因为当它变得太大而不适合底层类型时,整数开始操纵内存周围的内存。 所以我决定在Visual Studio 2015中编写一个测试程序,用以下代码测试该理论: #include <stdio.h> #include <
I was writing a program in C++ to find all solutions of ab = c, where a, b and c together use all the digits 0-9 exactly once. The program looped over values of a and b, and ran a digit-counting routine each time on a, b and ab to check if the digits condition was satisfied. However, spurious solutions can be generated when ab overflows the integer limit. I ended up checking for this using co
我正在用C ++编写一个程序来查找ab = c的所有解,其中a,b和c一起使用所有数字0-9。 程序循环遍历a和b的值,并且每次在a,b和ab上运行一个数字计数例程,以检查数字条件是否满足。 但是,当ab溢出整数限制时,可能会生成伪解。 我结束了检查这个使用代码,如: unsigned long b, c, c_test; ... c_test=c*b; // Possible overflow if (c_test/b != c) {/* There has been an overflow*/} else c=c_test; // No
We've all heard the warnings that if you invoke undefined behaviour in C or C++, anything at all can happen. Is this limited to any runtime behaviour at all, or does this also include any compile-time behaviour? In particular, is a compiler, upon encountering a construct that invokes undefined behaviour, allowed to reject the code (in the absence of other requirements in the standard to do
我们都听到过警告,如果你用C或C ++调用未定义的行为 ,任何事情都可能发生。 这是否仅限于任何运行时行为,还是包含任何编译时行为? 特别是,编译器在遇到一个调用未定义行为的构造时,是否允许拒绝代码(在标准中没有其他要求的情况下这样做),甚至是崩溃? “ 你们都忽略了实际的定义,并专注于笔记,该标准没有要求。 ” - @ R.MartinhoFernandes 上面的消息是由给定用户在Lounge <C ++>中编写的,并且是一个
Consider the following statement: *((char*)NULL) = 0; //undefined behavior It clearly invokes undefined behavior. Does the existence of such a statement in a given program mean that the whole program is undefined or that behavior only becomes undefined once control flow hits this statement? Would the following program be well-defined in case the user never enters the number 3 ? while (true)
考虑以下声明: *((char*)NULL) = 0; //undefined behavior 它清楚地调用未定义的行为。 在给定的程序中是否存在这样的陈述意味着整个程序是不确定的,或者只有当控制流程达到这个陈述时行为才变得不确定? 如果用户从不输入数字3 ,以下程序是否定义明确? while (true) { int num = ReadNumberFromConsole(); if (num == 3) *((char*)NULL) = 0; //undefined behavior } 或者,无论用户输入什么内容,它完全是未定
After reading this discussion I realized that I almost totally misunderstand the matter :) As the description of C++ abstract machine is not rigorous enough(comparing, for instance, with JVM specification), and if a precise answer isn't possible I would rather want to get informal clarifications about rules that reasonable "good" (non-malicious) implementation should follow. The
读完这个讨论后,我意识到我几乎完全误解了这个问题:) 由于C ++抽象机器的描述不够严谨(例如,与JVM规范进行比较),并且如果不可能得到精确的答案,我宁愿要获得有关规则的非正式说明,即合理的“良好”(非恶意的)应该遵循。 标准的第1.9部分关于执行自由的关键概念就是所谓的if规则: 只要结果就好像该要求已被遵守一样,只要可以从该程序的可观察行为中确定,实施就可以自由地忽视本标准的任何要求。 根据标准(我引
I think about conditionals and compilers. I am programming an application for Arduino and so I need the application to be as fast as possible. In my code I have this: #define DEBUG false ... if (DEBUG) { String pinName; pinName = "Pin "; pinName += pin; pinName += " initialized"; Serial.println(pinName); } I am wondering if the compiler does not include the code (code in if b
我考虑条件和编译器。 我正在为Arduino编写应用程序,因此我需要应用程序尽可能快。 在我的代码中,我有这样的: #define DEBUG false ... if (DEBUG) { String pinName; pinName = "Pin "; pinName += pin; pinName += " initialized"; Serial.println(pinName); } 我想知道如果编译器不包含二进制文件中的代码(代码在if块)。 条件总是错误的,所以程序永远不会去那里。 从另一面。 如果DEBUG是真的
In the stack, memory is reserved for main which we call stack frame for the main function. When we call the Add function, memory is reserved on top of the stack. In the Add function stack frame, a and b are local pointers and c is an integer which calculates the sum and then we return the reference. c is a local variable of Add function. Now when the Add function execution is completed the
在堆栈中,存储器被保留用于main我们称之为堆栈帧main功能。 当我们调用Add函数时,内存保留在堆栈顶部。 在Add函数堆栈框架中, a和b是局部指针, c是一个计算总和的整数,然后返回参考。 c是Add函数的局部变量。 现在,当Add函数执行完成时,堆栈中的内存空间也会被释放,所以当我们尝试访问main指针p中的这个地址时,我们试图访问的基本上是一个释放空间。 编译器会给出一个警告,但为什么它仍然正确输出值5? 答案