具有 asio get 方法成员的 C++ 对象

标签 c++ class member ostream

我想实现一个成员匹配这个函数的类:

int get(
  string &host_,
  string &port_, 
  string url_path,
  ostream &out_,
  vector<string> &headers, 
  unsigned int timeout
  )

我有这个:

#include <string>
#include <vector>
#include <iostream>
#include <istream>
#include <ostream>
#include "Register.h"

using namespace std;

class Request : public Register {

private:
string *host;
string *port;
string *url;
ostream *out;
vector<string> *header;
unsigned int *timeout;

public:
Request() {
  this -> host = new string();
  this -> port = new string();
  this -> url = new string();
  this -> out = new ostream();
  this -> header = new vector<string>();
  this -> timeout = new int();
}

但我无法实例化它。例如,ostream 是怎么回事:

this -> out = new ostream();

我对 c++ 还是个新手,现在我完全糊涂了,我在谷歌上找不到正确的答案。

最佳答案

std::ostream是特定流实现的基类,例如 std::ofstream ,你不能直接实例化它。

在 C++ 中,与 Java 不同,我们不会在堆上分配所有内容,而是更喜欢值语义:

/// Can be easily copied
class HoldSomething {
private:
    std::string name;
    std::vector<std::string> values;
public:
    /// constructors
    HoldSomething() : name(), values() {}
    explicit HoldSomething( const std::string& name ) :
        name( name ), values() {}
    /// modifiers
    void addValue( const std::string& val ) {
        value.push_back( val );
    }
...

关于具有 asio get 方法成员的 C++ 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11902437/

相关文章:

c++ - 在主类之外声明多个嵌套类

python - 理解Python中的父/子类

c++ - std::string 作为成员函数参数的奇怪行为

c++ - 奇怪的 C/C++ 语法

jquery - 使用类和 ID 的正确方法

Javascript - 将对象的成员函数放在独立变量中?

C++ 成员函数使用另一个成员函数

c++ - 如何在 Visual C++ 项目中构造文件/依赖项

c++ - 谷歌测试中的数组比较?

c++ - 如何使用英特尔内在移动数组的元素