假设我有这些课程。
资源.h
template<typename T>
class Resource
{
protected:
// bunch of member variable
std::unique_ptr<T> resource;
public:
Resource();
~Resource();
};
Run Code Online (Sandbox Code Playgroud)
资源管理器
template<typename T>
class ResourceManager
{
protected:
std::unordered_map<std::string, Resource<T>> resources;
public:
ResourceManager();
~ResourceManager()
{
resources.clear();
}
};
Run Code Online (Sandbox Code Playgroud)
resources.clear()从地图上删除所有元素,并调用对象的析构函数。我的问题是,unique_ptr是自己取消分配的,还是如果没有分配,我们是否需要做一些事情来取消分配它们?
我正在尝试创建一个派生类对象并使用现有的基类对象填充数据成员(来自基类)。
\n\n在我的流程生命周期中,我已经有一个基类对象。对于某些决定,我必须创建一个新对象(从基类派生)。实现此目的的一种方法是公开评估者并复制数据。有没有我可以使用聚合初始化或dynamic_cast之类的解决方案?
\n\n#include <iostream>\n#include <string>\n\nusing namespace std;\n\nclass Base {\n protected: \n string name_;\n\n public:\n Base() : name_ ("foo")\n {\n }\n void ChangeName()\n {\n name_ = std::string {"bar"};\n }\n};\n\nclass Child final : Base {\n public:\n string GetName()\n {\n return name_;\n }\n};\n\nint main()\n{\n Base b;\n b.ChangeName();\n\n Child c = {b};\n cout<<"Hello World. Here is my name: " << c.GetName() << endl;\n\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n预期输出:\n 你好世界。这是我的名字:酒吧
\n
编译错误
\n\ntry.cpp:33:17: error: could not convert \xe2\x80\x98{b}\xe2\x80\x99 from \xe2\x80\x98<brace-enclosed initializer …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 OpenGL (我使用 GLFW 和 GLEW 库)在 C ++ 中编写代码。这是代码:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#define numVAOs 1
GLuint renderingProgram;
GLuint vao[numVAOs];
GLuint createShaderProgram(){
const char*vshaderSource =
"#version 430 \n"
"void main(void) \n"
"{gl_Position = vec4(0.0,0.0,0.0,1.0)};";
const char*fshaderSource =
"#version 430 \n"
"out vec4 color; \n"
"void main(void) \n"
"{gl_Position = vec4(0.0,0.0,1.0,1.0)};";
GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vShader,1,&vshaderSource,NULL);
glShaderSource(fShader,1,&fshaderSource,NULL);
glCompileShader(vShader);
glCompileShader(fShader);
GLuint vfProgram = glCreateProgram();
glAttachShader(vfProgram,vShader);
glAttachShader(vfProgram,fShader);
glLinkProgram(vfProgram);
return vfProgram;
}
int main()
{
glfwInit();
// Define version …Run Code Online (Sandbox Code Playgroud) 我创建了简单的C ++ std::string值。
但是该值会产生意想不到的结果。
我使用g ++编译器(Linux)和Visual Studio(Windows)测试了此代码,并且两个编译器都出现相同的问题。
正常结果码
/* This code results are Normal */
#include <bits/stdc++.h>
int main() {
std::string a1 = "a1";
std::string a2 = "a2";
std::string b1("b1");
std::string b2("b2");
const char *c1 = std::string("c1").c_str();
const char *c2 = std::string("c2").c_str();
std::cout << "Expected [a1], real [" << a1 << "]\n";
std::cout << "Expected [a2], real [" << a2 << "]\n";
std::cout << "Expected [b1], real [" << b1 << "]\n";
std::cout << "Expected [b2], real [" << …Run Code Online (Sandbox Code Playgroud) 您好,我正在尝试学习如何在Linux上使用x86_64程序集进行系统调用。我遇到了一个问题,我似乎无法弄清楚如何正确地传递的参数getpeername。
在使用C的此链接中,似乎他们正在使用运算符的地址来传递参数。我不知道如何在程序集中复制它。当我在缓冲区中不使用方括号时,这是strace。
首先,我在.data节中定义了我的缓冲区
ip_buff: times 14 db 0
.length: equ $-ip_buff
Run Code Online (Sandbox Code Playgroud)
这是一个宏
%define SYS_getpeername 52
Run Code Online (Sandbox Code Playgroud)
r12 存储套接字接受调用的返回值
syscall getpeername,r12,ip_buff,15
Run Code Online (Sandbox Code Playgroud)
这是不使用方括号的strace
[pid 749] accept(3, NULL, NULL <unfinished ...>
[pid 761] read(4, "", 1024) = 0
[pid 761] write(1, "", 0) = 0
[pid 761] getpeername(4, 0x600733, 0xf) = -1 EFAULT (Bad address)
Run Code Online (Sandbox Code Playgroud)
这是我使用方括号时的痕迹。
[pid 749] accept(3, NULL, NULL <unfinished ...>
[pid 745] read(4, "GET / HTTP/1.1\r\nHost: 127.0.0.1:"..., 1024) = 78
[pid 745] write(1, "GET / HTTP/1.1\r\nHost: 127.0.0.1:"..., 78) = …Run Code Online (Sandbox Code Playgroud) 我正在尝试Math.clamp使用内部汇编程序在V8中实现我自己的版本。
我设法使用Torque脚本语言生成了它的一个版本,并希望尝试使用CodeStubAssembler内置插件进行类似的实现。我意识到他们被认为是老派,但这似乎是生成我想要的实际装配的最简单方法。目的是使编译器生成的代码与此尽可能相似
问题是:是否有已经产生cmp并产生cmov指令的汇编程序?如果没有,我将如何生成它?
这仅出于教育目的,这就是为什么我仅对x64管道感兴趣并将所有输入都视为整数的原因。
据我所知,std::allocator是由库引入的,用于分配未初始化的未构造内存块。所以:
std::allocator<int> a;
auto ptr = a.allocate(100);
auto e = ptr;
while (e != ptr + 5)
a.construct(e++, 0);
for (auto tmp = ptr; tmp != e; )
std::cout << *tmp++ << ", ";
std::cout << std::endl;
std::allocator<int> a2;
std::allocator<int> a3 = a;
for (auto tmp = ptr; tmp != e; )
a.destroy(tmp++);
//for (auto tmp = ptr; tmp != e; )
// a2.destroy(tmp++); // is it UB using a2 here to destroy elements?
//for (auto tmp = …Run Code Online (Sandbox Code Playgroud) 我知道已经有很多人对此有疑问,所以请耐心听我解答这一问题。
于是我发现了这个问题,对修改这个有疑问。
class Blah {
public:
Blah();
Blah(int x, int y);
int x;
int y;
Blah operator =(Blah rhs);
};
Blah::Blah() {}
Blah::Blah(int xp, int yp) {
x = xp;
y = yp;
}
Blah Blah::operator =(Blah rhs) {
x = rhs.x;
y = rhs.y;
return *this;
}
int main() {
Blah b1(2, 3);
Blah b2(4, 1);
Blah b3(8, 9);
Blah b4(7, 5);
b3 = b4 = b2 = b1;
cout << b3.x << ", " << b3.y …Run Code Online (Sandbox Code Playgroud) 我对 C++ 中指针的行为有点好奇。所以我有这个小程序要测试,不幸的是它运行良好。
#include <iostream>
class Test{
public:
Test(){
std::cout<<"Test Created!\n";
}
~Test(){
std::cout<<"Test Destroyed!\n";
}
};
void destroyer(Test* T){
Test* temp = T;
delete temp;
}
int main(){
Test* ptr = new Test();
destroyer(ptr);
}
Run Code Online (Sandbox Code Playgroud)
它给予回报
Test Created!
Test Destroyed!
Run Code Online (Sandbox Code Playgroud)
而且我得出的结论是,当我们删除一个指针时,实际上只是删除了指针所指的对象,而不是变量本身(变量指针会在程序结束时自动删除,与其他原始数据类型相同)。我的想法是真的吗?
我正在编写一个库,它对内置类型(int、float、double 等)和用户提供的类型执行一些操作。其中之一是由模板函数执行的:
namespace lib
{
template<typename T>
inline auto from_string(std::string const & s, T & t) -> bool
{
std::istringstream iss(s);
iss >> t;
return !iss.fail();
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个定制点 - 用户可以根据自己的类型重载此函数:
namespace foo
{
class UserType
{
// (...)
};
}
namespace lib
{
inline auto from_string(std::string const & s, foo::UserType & ut) -> bool
{
// some implementation
}
}
Run Code Online (Sandbox Code Playgroud)
或者将from_string函数放在同一命名空间中并通过 ADL 访问:
namespace foo
{
inline auto from_string(std:string const & s, UserType & ut) -> bool …Run Code Online (Sandbox Code Playgroud)