class MyClass
{
public:
~MyClass() {}
MyClass():x(0), y(0){} //default constructor
MyClass(int X, int Y):x(X), y(Y){} //user-defined constructor
MyClass(const MyClass& tempObj):x(tempObj.x), y(tempObj.y){} //copy constructor
private:
int x; int y;
};
int main()
{
MyClass MyObj(MyClass(1, 2)); //user-defined constructor was called.
MyClass MyObj2(MyObj); //copy constructor was called.
}
Run Code Online (Sandbox Code Playgroud)
在第一种情况下,当MyClass(1, 2)调用用户定义的构造函数并返回一个对象时,我希望MyObj调用复制构造函数.为什么它不需要为第二个实例调用复制构造函数MyClass?
我刚刚从VS2005迁移到VS2010,它无法编译一个简单的程序.
#include <iostream>
using std::cout;
using std::endl;
int main()
{
cout << "Hello Visual Studio 2010 :)" << endl;
}
Run Code Online (Sandbox Code Playgroud)
错误 -
1 error TRK0005: Failed to locate: "CL.exe". The system cannot find the file specified.
2 IntelliSense: cannot open source file "iostream"
3 IntelliSense: name followed by '::' must be a class or namespace name
4 IntelliSense: name followed by '::' must be a class or namespace name
5 IntelliSense: identifier "cout" is undefined
6 IntelliSense: identifier "endl" is …Run Code Online (Sandbox Code Playgroud) 我正在node.js中编写聊天服务器,我想将连接用户的IP地址存储在mysql数据库中作为(无符号)整数.我编写了一个javascript方法将ip-address转换为字符串为整数.然而,我得到一些奇怪的结果.
这是我的代码:
function ipToInt(ip) {
var parts = ip.split(".");
var res = 0;
res += parseInt(parts[0], 10) << 24;
res += parseInt(parts[1], 10) << 16;
res += parseInt(parts[2], 10) << 8;
res += parseInt(parts[3], 10);
return res;
}
Run Code Online (Sandbox Code Playgroud)
当我运行调用方法时,ipToInt("192.168.2.44");我得到的结果是-1062731220.看起来好像发生了溢出,这很奇怪,因为预期的输出(3232236076)在javascript(2 ^ 52)的数字范围内.
当我-1062731220以二进制形式检查时,我可以看到它3232236076被保留了,但是充满了前导1.
我不确定,但我认为问题在于有符号和无符号整数.
你们任何人都可以解释发生了什么吗?并可能如何解析-1062731220回字符串IP?
我试图在类中声明一个回调函数,然后在某处我读取函数需要是静态的,但它没有解释为什么?
#include <iostream>
using std::cout;
using std::endl;
class Test
{
public:
Test() {}
void my_func(void (*f)())
{
cout << "In My Function" << endl;
f(); //Invoke callback function
}
static void callback_func()
{cout << "In Callback function" << endl;}
};
int main()
{
Test Obj;
Obj.my_func(Obj.callback_func);
}
Run Code Online (Sandbox Code Playgroud) 我正在读这篇文章,提到破坏者是微不足道的,不平凡的.
如果一个类具有明确定义的析构函数,或者它具有成员对象或具有非平凡析构函数的基类,则该类具有非平凡的析构函数.
在示例中,我有一个班级,
class C {
public:
~C(); // not explicitly declared.
};
Run Code Online (Sandbox Code Playgroud)
如果C::~C()隐式定义它是否会产生一个trival dtor?
编译器是否为自我赋值生成了赋值操作符?
class T {
int x;
public:
T(int X = 0): x(X) {}
};
int main()
{
T a(1);
a = a;
}
Run Code Online (Sandbox Code Playgroud)
即使班级成员不是指针类型,我是否总是需要防止自我分配?
重载&&,||是一个坏主意 或逗号运算符和为什么?
当您使用指针在堆上分配动态内存时,
char *buffer_heap = new char[15];
Run Code Online (Sandbox Code Playgroud)
它将在内存中表示为:
ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍýýýý««««««««þþþ
Run Code Online (Sandbox Code Playgroud)
为什么最后没有一个NULL终止字符而不是««««««««þþþ?
我遇到的问题是SDL表示它不支持OpenGL 3.x上下文.我正在尝试遵循本教程:在SDL(C/SDL)中创建跨平台OpenGL 3.2上下文.我在这种情况下使用GLEW,但我也无法使用gl3.h来处理这个问题.这是我最终得到的代码:
#include <glew.h>
#include <SDL.h>
int Testing::init()
{
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
DEBUGLINE("Error initializing SDL.");
printSDLError();
system("pause");
return 1; // Error
}
//Request OpenGL 3.2 context.
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
//set double buffer
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
//Create window
window = SDL_CreateWindow("OpenGL 3.2 test",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
600, 400, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if(window == NULL) return 3; // Error
//Print errors to console if there are any
printSDLError(__LINE__);
//Set up OpenGL context.
glContext = SDL_GL_CreateContext(window);
printSDLError(__LINE__);
if(glContext == NULL) …Run Code Online (Sandbox Code Playgroud) c++ ×9
callback ×1
compilation ×1
destructor ×1
javascript ×1
opengl ×1
opengl-3 ×1
operators ×1
sdl ×1
visual-c++ ×1