C++
walsons
此人不懒
展开
-
C++类内定义友元却无需类外声明的特殊情况
这样的代码并不generic,原因就是类模板作为参数可以有很多不同的类型,因此,如果友元函数使用了该类作为参数,则不需要在类外声明。将一个全局函数作为某个类的友元时,可以在类内定义函数,但必须在类外对函数进行声明。但有一种特殊情况并不需要再次声明。在上述例子中,四个函数都没有在类外进行声明,但只有。调用会出错,其他函数调用正常。因此如果想在类外进行声明时,需要对不同。每次实例化的时候,会同时实例化一个。函数,注意,operator<<函数的形参列表中不包含。原创 2023-10-27 14:16:16 · 144 阅读 · 0 评论 -
std::thread参数传递
std::thread参数传递std::thread中的参数传递可以用下图解释,首先看代码#include <iostream>#include <thread>class Base{public: Base() { std::cout << "Base default constructor" << std::endl; } Base(const Base &base) {原创 2022-01-02 23:46:50 · 1440 阅读 · 0 评论 -
Windows制作动态库(.dll)
文件目录如下:将项目dll_provider的Configuration Type设置为Dynamic Library (.dll)文件内容如下:IInterface.h#ifndef IINTERFACE_H_#define IINTERFACE_H_#include <windows.h>#define XEXPORT __declspec(dllexport)extern "C"{ XEXPORT void __cdecl Print(const char *s)原创 2021-12-27 17:08:05 · 680 阅读 · 0 评论 -
C++实现HTTP断点传输视频流
C++实现HTTP断点传输视频流Linux环境下socket编程实现,采用epoll实现并发,代码直接复制可用编译g++ server.cc -o server -Wall -std=c++11运行./server port file_path # example: # ./server 12000 video.mp4 代码及注释#include <sys/socket.h>#include <arpa/inet.h>#include <s原创 2021-07-12 11:36:53 · 1535 阅读 · 0 评论 -
C语言文件操作
C语言和C++文件操作C语言C++C语言//头文件#include <stdio.h>/* * 打开文件, 打开失败返回 NULL * FILE: 系统定义的一个数据结构,含有文件名、文件状态、文件当前位置等信息 * filename: 文件名 * modes: * "r": 打开文本文件,进行读操作 * "w": 打开文本文件,进行写操作 * "a": 向文本文件追加数据 * "r+": 打开文本文件,进行读/写操作 * "w+": 打开文本文件,进行读/写操作原创 2021-05-08 14:19:40 · 99 阅读 · 0 评论 -
C++派生类的拷贝或移动构造函数
C++派生类的拷贝或移动构造函数1. 当派生类定义了拷贝或移动构造函数,必须显式地调用基类的拷贝或移动构造函数,否则会调用基类的默认构造函数#include <iostream>class Base { public: Base() { std::cout << "Base default constructor" << std::endl; } Base(const Base &b) { std::cout << "Base cop原创 2021-04-23 16:04:24 · 2481 阅读 · 0 评论 -
C++查看模板实例化解决指针问题
C++查看模板实例化解决指针问题查看模板实例化的方法遇到的指针问题解决思路查看模板实例化的方法clang++ -Xclang -ast-print -fsyntax-only example.cc其中example.cc是想查看模板实例化的文件遇到的指针问题#include <iostream>template <typename T> void debug_rep(const T &t) { std::cout << "func1" <原创 2021-03-18 21:09:25 · 1301 阅读 · 0 评论 -
关于C++中 stod(s, p) 中 p 的用法
关于C++中 stod(s, p) 中 p 的用法#include <iostream>using namespace std;#include <string>int main(){ string s("123.44eee"); size_t num; size_t *p = # double a = stod(s, p); cout << a <<原创 2020-11-15 10:17:46 · 981 阅读 · 0 评论 -
C++多态中虚函数的默认参数问题
C++多态中虚函数的默认参数问题1.基类中虚函数的默认参数会在编译过程就被保存,再调用子类的函数后发生多态,编译器会使用基类的默认参数。#include<iostream>using namespace std;class Base {public: virtual void func(int i = 0) { cout << "Person" << i << endl; }};class Son :public Base{publ原创 2020-06-14 14:44:53 · 1480 阅读 · 0 评论 -
如何理解:静态数据成员的类型可以就是它所属的类类型
#include<iostream>using namespace std;class Bar {public: static Bar bar; int a; double b;};Bar Bar::bar = { 12, 13.2 };int main(){ Bar obj = { 1, 3.15 }; cout << obj.a << endl; cout << obj.bar.a << endl; cout.原创 2020-10-23 22:41:33 · 579 阅读 · 0 评论 -
(a = b) = 1中(a = b)的返回值 (C++)
(a = b) = 1中(a = b)的返回值 (C++)先看如下一段代码;#include<iostream>using namespace std;int main() { int a; int b = 5; (a = b) = 1; cout << a << endl; cout << b << endl; system("pause"); return 0;}输出的a, b分别是多少?运行结果:原创 2020-09-30 09:41:28 · 1143 阅读 · 0 评论 -
VS引发了异常:读取访问权限冲突。p是nullptr(C++)
VS引发了异常:读取访问权限冲突。p是nullptr(C++)在写链表的时候有时会遇到以下问题:代码如下:#include<iostream>using namespace std;typedef struct stru { int data; struct stru *next;} LinkList;void CreateListR(LinkList *&L, int arr[], int n) { L = new LinkList; L->next原创 2020-08-02 10:09:44 · 27181 阅读 · 2 评论 -
C/C++中“指向结构体成员运算符”使用注意事项
C/C++中“->”使用注意事项“->”运算符叫做“指向结构体成员运算符”,是C语言和C++语言的一个运算符。使用时应注意符号左边应该为指针。#include<iostream>using namespace std;struct person { int *age = new int(10);};int mian(){ person p; //cout << p->age << endl; cout << *p.a原创 2020-06-11 17:55:20 · 1248 阅读 · 0 评论 -
C++链式调用函数注意事项
C++链式调用函数注意事项链式调用函数时,传入的参数值是之前的值#include<iostream>using namespace std;class Dog {public: Dog() :m_age(10) {} Dog& function1(Dog gp) { m_age += gp.m_age; return *this; } int m_age;};void func() { Dog g1; cout << g1.m_age &原创 2020-06-10 15:49:21 · 1341 阅读 · 2 评论