小编cao*_*ang的帖子

从整数转换为指针

我正在学习使用C++进行投射,之后的代码对我来说是神奇的.

#include <iostream>
using namespace std;

class Base {
public:
virtual void f() { }
};

#define SOME_VALUE 8

int main() {
cout << SOME_VALUE <<endl;
getchar();
}
Run Code Online (Sandbox Code Playgroud)

输出是:8

代码很简单,但SOME_VALUE是什么类型的?int,或double还是char?

之后更复杂:

#include <iostream>
using namespace std;

class Base {
public:
virtual void f() { }
};

#define SOME_VALUE 8
int main() {
cout << (Base*)SOME_VALUE-SOME_VALUE <<endl;
getchar();
}
Run Code Online (Sandbox Code Playgroud)

输出为:FFFFFFE8

遵循此代码,我可以理解SOME_VALUE是数字类型.我也测试sizeof(SOME_VALUE),输出为4.但如果SOME_WHAT是数字,它如何更改为对象指针?对象指针如何减去整数?

c++ pointers integer casting c-preprocessor

1
推荐指数
1
解决办法
309
查看次数

为什么Base类和Drive类在启动Drive实例时具有相同的虚拟指针但是2 vtable

#include <iostream>
using namespace std;

class Base {
public:
    Base() {
        cout << "In Base" << endl;
        cout << "Virtual Pointer = " << (int*)this << endl;
        cout << "Address of Vtable = "
        << (int*)*(int*)this << endl;
        cout << "Value at Vtable = "
        << (int*)*(int*)*(int*)this << endl;
        cout << endl;
    }

    virtual void f1() { cout << "Base::f1" << endl; }
};

class Drive : public Base {
public:
    Drive() {
        cout << "In Drive" << endl;
        cout …
Run Code Online (Sandbox Code Playgroud)

c++ virtual inheritance pointers vtable

1
推荐指数
1
解决办法
1039
查看次数

无法使用非常简单的 httpWebRequest 类应用程序连接远程服务器

我使用 Visual2010 编写了一个带有 httpWebRequest 类的简单应用程序。第一次运行该应用程序时,它可以工作,但在取得一些成功后,它会出现警告 “无法连接远程服务器”。 我在网上读了很多资料,但没有太多线索,几乎可以说是因为防病毒软件或防火墙导致了问题,但当我关闭两者时,它仍然不起作用。我也重新安装了Visual2010,但问题仍然存在

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace new_httpWebRequest
{
class Program
{
    static void Main(string[] args)
    {
        string result ="";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://my-favor.net");
        // line code problem below:
        `HttpWebResponse response = (HttpWebResponse)request.GetResponse();`
        var sr = new StreamReader(response.GetResponseStream() ?? System.IO.Stream.Null, Encoding.UTF8);
        result = sr.ReadToEnd();
        sr.Close();
        Console.Write(result);
        Console.ReadLine();

    }
}
Run Code Online (Sandbox Code Playgroud)

}

httpwebrequest connect visual-studio-2010

1
推荐指数
1
解决办法
4352
查看次数