小编Hem*_*ava的帖子

运算符"<<"重载返回类型

假设有一个cPoint类.

class cPoint {
  int x, y, z;
};
Run Code Online (Sandbox Code Playgroud)

我想在一个语句中打印所有三个变量.所以,我重载了运算符<<就像

   friend std::ostream& operator<< (std::ostream &cout, cPoint &p);

   std::ostream& operator<< (std::ostream &out, cPoint &p) {
     out << p.get_x() << " " << p.get_y() << " " << p.get_z() << std::endl;
     return out;
   }
Run Code Online (Sandbox Code Playgroud)

合理?

我的问题在于插入运算符(>>)会发生什么.我也重载了它,将x,y和z的值转换为单个语句.

    friend std::istream& operator>> (std::istream &cin, Point &p);

    std::istream& operator>> (std::istream &in, Point &p) {
        int tmp;
        in >> tmp;
        p.set_x(tmp);
        in >> tmp;
        p.set_y(tmp);
        in >> tmp;
        p.set_z(tmp);
    }
Run Code Online (Sandbox Code Playgroud)

明确?

int main() {
  cout << p …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

如何在Perl中获取全局变量值?

试图了解范围解析运算符.

$a = 5;
foo();
print "Out a = $a\n";

sub foo() {
  local $a = 10;
  bar();
}

sub bar() {
  print "Inside a = $a\n";
  print "Global a = $::a\n";
}
Run Code Online (Sandbox Code Playgroud)

该计划的输出是:

Inside a = 10
Global a = 10
Out a = 5
Run Code Online (Sandbox Code Playgroud)
  1. 我希望'$ :: a'的值可以表示为'5'而不是10.我认为这就是范围解析运算符的作用.获取范围值.在这种情况下,没有给出范围,因此具有全球价值.如果有任何调整,请纠正我.

  2. 我应该写什么来获得bar子程序中'a'的全局值?

perl

3
推荐指数
2
解决办法
822
查看次数

verilog 中的总线表示法

我一直以为总线符号是这样注释的:

input bus[MSB:LSB]
Run Code Online (Sandbox Code Playgroud)

其中 MSB >= LSB。

但最近,我被告知这也是可能的:

wire LSB >= MSB.
Run Code Online (Sandbox Code Playgroud)

这是真的吗?

如果是这样,那么合成器工具如何获取总线大小等?他们是否认为哪个指数大就是MSB?

verilog system-verilog

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

比较许多const char*和一个const char*

今天可能是基本的.

我有一个输入作为const char*,我想将它与const char*的许多选项进行比较.像我在下面写的那样.

str_equal - 将其视为const char*的比较函数.'str'是输入const char*.

所以,写成这样:

bool function1(const char* str) {
  if (str_equal(str, "abc")) {
    // Do for abc
  } else if (str_equal(str, "def")) {
    // Do for def
  } else if (str_equal(str, "ghi")) {
    // Do for ghi
  } ...
  ...
}
Run Code Online (Sandbox Code Playgroud)

想知道实现这一目标的其他可行方法是什么.如你所见,它看起来非常难看.

c

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

如何处理以2D数组编写的条件

考虑这个二维数组:

  A B C D E
A 1 0 0 0 0
B 0 1 1 0 0
C 0 1 1 0 0
D 0 0 0 1 1
E 0 0 1 1 1
Run Code Online (Sandbox Code Playgroud)

这意味着A 可以与A映射,但是sureshot不能与B,C,D或E映射。

同样,B 可以与B和C映射,而不能与A,D和E映射。

这可能意味着它可能已映射或未映射。

现在,我必须编写一个c ++代码以确保此映射成立并断言是否成立。

我已经写了一个关于这个(骨骼)的if否则代码。我正在检查B不能确定射击是否匹配的条件。

if (checking_for_B) {
  if (B is mapped with A || B is mapped with D || B is mapped with E) {
    assert();
  }
}
Run Code Online (Sandbox Code Playgroud)

同样,我也必须为A,B,C,D和E编写if条件。我对这种方法不满意。如果你是我,你会写什么?

c++ algorithm matrix

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

将 g++ 编译器的输出重定向到文件?

我在编译 C++ 程序时看到以下消息。

\n
 test.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\n test.cpp:950: error: no match for \xe2\x80\x98operator<<\xe2\x80\x99 in \xe2\x80\x98std::cout << d->Derived::print()\xe2\x80\x99\n /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]\n /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:117: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]\n
Run Code Online (Sandbox Code Playgroud)\n

我在想是否可以将所有这些消息重定向到一个文件,并且在控制台上看不到任何内容。我尝试通过以下方式重定向 stderr 消息

\n
g++ test.cpp 2> xx\n
Run Code Online (Sandbox Code Playgroud)\n

但这似乎不起作用。我仍然在控制台上看到所有内容,但文件内什么也没有。

\n

linux g++ tcsh

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

为什么建议在运算符重载中将函数声明为“友元”

我在很多地方读过这篇文章,建议在重载运算符时使用“friend”,但没有人清楚地解释为什么真正需要它?为什么我们不能将它们声明为普通成员函数?有什么缺点吗?

谷歌搜索但没有得到任何明确的答案。

c++ c++11

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

unique_ptr 中的构造函数被删除

正在阅读有关智能指针的更多信息,并遇到了当您将一个 unique_ptr 复制到另一个时构造函数被删除的概念。这个概念到底是什么?

#include<iostream>
#include<memory>

class Person {
  public:
  int e;
  Person(int e) : e(e) { }
};

int main() {
  std::unique_ptr<Person> p (new Person(5));
  // Below line seems to be deleting constructor and thus error in compiling.
  std::unique_ptr<Person> q  = p;
}
Run Code Online (Sandbox Code Playgroud)

不过 std::move 语义工作正常。

c++ unique-ptr

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

获取变量的中间范围值

假设这是一段代码,用于演示如何使用本地和全局变量.我试图向一个初级家伙解释这个,他问我这个问题.

在下面的代码中,你应该怎么做才能从外部循环中获取'x'的值.在这种情况下,如何访问其值为2的'x'.

#include<iostream>
using namespace std;
int x = 1;
void fun() {
  int x = 2;
  {
    int x = 3;
    cout << x << endl; // This will give 3
    cout << ::x << endl; // This will give 1
    // What should I write here to get x = 2.
  }
}
int main() {
  fun();
}
Run Code Online (Sandbox Code Playgroud)

c++

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

如何编写函数指针比调用它们更好?

我很高兴最近使用函数指针.我知道它们是如何工作的.函数指针的经典示例是:

int add() {
  return (100+10);
}
int sub() {
  return (100-10);
}

void print(int x, int y, int (*func)()) {
  printf("value is : %d", (x+y+(*func)()));
}

int main() {
  int x=100, y=200;
  print(x,y,add);
  print(x,y,sub);
}
Run Code Online (Sandbox Code Playgroud)

前几天有人问我,它比调用(内部主要)更好:

print(add(x,y));
print(sub(x,y));
Run Code Online (Sandbox Code Playgroud)

我努力解释这一点.它只是关于堆栈还是还有其他东西躺在下面?

c c++ function-pointers

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