cout

何时使用虚拟析构函数?

北战南征 提交于 2019-12-09 11:21:10
我对大多数面向对象理论有扎实的了解,但令我困惑的一件事是虚拟析构函数。 我以为无论链中的每个对象是什么,析构函数总是被调用。 您打算何时将它们虚拟化?为什么? #1楼 我喜欢考虑接口和接口的实现。 在C ++中,接口是纯虚拟类。 析构函数是接口的一部分,有望实现。 因此,析构函数应该是纯虚拟的。 构造函数呢? 构造函数实际上不是接口的一部分,因为对象总是显式实例化的。 #2楼 虚拟构造函数是不可能的,但虚拟析构函数是可能的。 让我们尝试一下... #include <iostream> using namespace std; class Base { public: Base(){ cout << "Base Constructor Called\n"; } ~Base(){ cout << "Base Destructor called\n"; } }; class Derived1: public Base { public: Derived1(){ cout << "Derived constructor called\n"; } ~Derived1(){ cout << "Derived destructor called\n"; } }; int main() { Base *b = new Derived1(); delete b; } 上面的代码输出以下内容:

C++: string.find()的使用

跟風遠走 提交于 2019-12-09 09:55:40
string.find(substring)返回substring在string中第一次出现的位置,如果没有找到则返回std::string::npos。 示例1 #include <iostream> int main() { std::string str("abcabcabcd"); std::cout << str.find("a") << std::endl; std::cout << str.find("bc") << std::endl; std::cout << str.find("za") << std::endl; // 找不到,返回string::npos } 编译运行: $ g++ -Wall test.cpp $ ./a.out 0 1 18446744073709551615 在一些编程语言中,找不到就返回-1,不过这里返回了一个很大的数字。 关于string.find()的返回类型 #include <iostream> int main() { std::string str("abcabcabcd"); std::string::size_type pos1 = str.find("ax"); std::size_t pos2 = str.find("ax"); int pos3 = str.find("ax"); std::cout <<

printf vs. std::cout [duplicate]

你。 提交于 2019-12-09 08:33:48
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Should I use printf in my C++ code? If I just want to print a string on screen, I can do that using those two ways: printf("abc"); std::cout << "abc" << std::endl; The case is, and in the examples shown above, is there an advantage of using printf over std::cout , or, vice versa? 回答1: While cout is the proper C++ way, I believe that some people and companies (including Google) continue to use printf in C++ code

How to create functions like std::cout?

℡╲_俬逩灬. 提交于 2019-12-09 02:14:18
问题 I'm creating my own logging utility for my project, I want to create a function like iostream's std::cout, to log to a file and print to the console as well. Here's what i want: enum { debug, error, warning, info }; LOG(level) << "test"; // level - from the above enum The result should be like this: int iPlayerID = 1337; LOG(info) << "Player " << iPlayerID << "Connected"; [Thu Jan 29 18:32:11 2015] [info] Player 1337 Connected 回答1: std::cout is not a function, it's an object of type std:

智能指针类模板——创建智能指针类模板

人盡茶涼 提交于 2019-12-08 21:47:27
#ifndef _SMARTPOINTER_H_ #define _SMARTPOINTER_H_ template < typename T > class SmartPointer { T* mp; public: SmartPointer(T* p = NULL) { mp = p; } SmartPointer(const SmartPointer<T>& obj) { mp = obj.mp; const_cast<SmartPointer<T>&>(obj).mp = NULL; } SmartPointer<T>& operator = (const SmartPointer<T>& obj) { if( this != &obj ) { delete mp; mp = obj.mp; const_cast<SmartPointer<T>&>(obj).mp = NULL; } return *this; } T* operator -> () { return mp; } T& operator * () { return *mp; } bool isNull() { return (mp == NULL); } T* get() { return mp; } ~SmartPointer() { delete mp; } }; #endif #include

Printing an uninitialized bool using cout (C++)

为君一笑 提交于 2019-12-08 16:52:08
问题 I have a class with a bool data member that is not initialized by the constructor. If I do cout << x.myBoolDataMember; where x is an object of this class in which the bool has not been initialized, I sometimes get a random number rather than 0 or 1. (I'm using gcc .) Is this behavior compliant with the Standard ? 回答1: Is this behavior compliant with the standard? Yes! Using garbage values(uninitialized) in your code invokes Undefined Behavior 回答2: Yes. An uninitialized variable can have any

C++ 进阶笔记之一

▼魔方 西西 提交于 2019-12-08 16:46:59
优化相关 使用灵活的、动态分配的数据,不要使用固定大小多数组; 优先使用线性算法或者尽可能快的算法: push_back 散列表查询:O(1) set/map lower_bound/upper_bound: O(logN) vector::insert for_each O(N) 尽可能避免劣于线性复杂性的算法,永远不要使用指数复杂性的算法; 不要进行不成熟的优化: 可以用通过引用传递的时候,却定义了通过值传递的方式传递参数; 可以使用前缀++、--运算的时候,却使用了后缀的方式; 构造函数中使用赋值操作而非初始化列表 并发编程 尽量减少全局和共享数据 避免使用名字空间作用域中具有外部连接的数据或者作为静态类成员的数据。如果不得不用,一定要对其仔细进行初始化。在不同编译单位中这种对象的初始化顺序是未定义的,为此需要高度注意;另外,名字空间作用域中的对象、静态数据成员对象或者跨线程(进程)共享的对象会减少并行性,往往是产生性能和可伸缩性瓶劲的原因。 例外情况: cin/cout/cerr 比较特殊: cout << "hello world" 等价于 (cout, "hello world"); 使用C++编写可靠的多线程的代码,认真考虑下面的建议: 参考目标平台的文档,了解改平台的同步原语,比如原子操作、内存屏障 最好将平台的原语用自己设计的抽象包装起来:比如使用pthread;

何时使用虚拟析构函数?

旧时模样 提交于 2019-12-08 14:23:28
我对大多数面向对象理论有扎实的了解,但令我困惑的一件事是虚拟析构函数。 我以为无论链中的每个对象是什么,析构函数总是被调用。 您打算何时将它们虚拟化?为什么? #1楼 我喜欢考虑接口和接口的实现。 在C ++中,接口是纯虚拟类。 析构函数是接口的一部分,有望实现。 因此,析构函数应该是纯虚拟的。 构造函数呢? 构造函数实际上不是接口的一部分,因为对象总是显式实例化的。 #2楼 虚拟构造函数是不可能的,但虚拟析构函数是可能的。 让我们尝试一下... #include <iostream> using namespace std; class Base { public: Base(){ cout << "Base Constructor Called\n"; } ~Base(){ cout << "Base Destructor called\n"; } }; class Derived1: public Base { public: Derived1(){ cout << "Derived constructor called\n"; } ~Derived1(){ cout << "Derived destructor called\n"; } }; int main() { Base *b = new Derived1(); delete b; } 上面的代码输出以下内容:

Write a simple Function that works like “std::cout” but adds a Line Break at the End

百般思念 提交于 2019-12-08 10:13:29
问题 In C++ there is a standard library function called cout , which lets me send text to the console. I am sure you know that. #include <iostream> using std::cout; cout << "Some Text " << 15 << " Other Text"; To do a linebreak at the end, I need to use endl . cout << "Some Text " << 15 << " Other Text" << endl; How can I write a function named coutl which behaves like cout but also adds a likebreak? I want to use the same syntax that cout uses, especially the << operator. coutl << "Some Text " <<

Logical operations in cout (C++)

落花浮王杯 提交于 2019-12-08 09:01:36
问题 Consider: int i = 56, j = 0; int n = i&&j; cout << i&&j; cout << endl << n; the output would be: 56 0 I imagine its either because of operator precedence or logical short circuit, but I can't seem to figure out which or the reason 回答1: The expression cout << i&&j is equivalent to (cout << i) && j . Both operands are evaluated and converted to bool . The statement as a whole has no effect, but the evaluation of the subexpression cout << i has the usual side effects, of course, namely writing