小编Tom*_*ica的帖子

如何在没有重复代码的情况下处理 getter 的 const/non const 组合?

假设我有一个结构体,它有一个名称和一些与之关联的值:

struct Season {
  std::string name;
  // Mean temperature over the season
  double meanTemperature;
  // Days since the start of year when it's strongest
  float strongestAt;
  // Fraction of a year it applies to, compared to other seasons
  float yearFraction;
}
Run Code Online (Sandbox Code Playgroud)

本课程描述了每年的一个季节。假设我有一个它们的集合,它填充了一整年:

// made up method that is supposed to find a season (I don't use it in real code)
int findIndex(const std::vector<Season>& in, std::function<bool(const Season&)>);
class SeasonCollection
{
public:
  const Season* GetSeason(const std::string& name) const
  {
    const int index …
Run Code Online (Sandbox Code Playgroud)

c++ code-reuse constants c++11

4
推荐指数
2
解决办法
106
查看次数

何时使对象自行删除?

Callback* p = new Callback;
function(p);
Run Code Online (Sandbox Code Playgroud)

如果我想删除回调对象,何时以及如何删除?

如果它被提前删除,那么回调可能会因分段错误而失败.

c++ oop memory-management

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

将图标添加到菜单项

我想添加ImageIcontoJMenuItem来说明诸如Newsave 之类的操作
为什么以下代码对我不起作用?

   JMenu file = new JMenu("File");
   menubar.add(file);
   JMenuItem newgame = new JMenuItem("New");
   file.add(newgame);
   newgame.setIcon(new ImageIcon("/Project1/zkre/new.gif"));
Run Code Online (Sandbox Code Playgroud)

java swing embedded-resource

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

将char*转换为double - 作为字节

我有一个代表double的字节数组:

char number[8];
Run Code Online (Sandbox Code Playgroud)

我需要将它转换为实际的double(也有8个字节).基于我尝试过的建议,但它失败了:

std::cout<<(*((*double)number))<<" is my number.\n";
Run Code Online (Sandbox Code Playgroud)

为什么会失败,我该怎么办?当然,我可以使用一些<<魔法提取数据,但我不想这样做 - 它会消耗内存并使代码过于健壮.

c++ double casting c-strings

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

3D矢量的角度 - 得到两者

我有一个速度对象A. 速度指定为3D矢量a = (x, y, z).位置是3D点A [X, Y, Z].我需要找出来,如果目前的速度导致该对象到另一个对象B上的位置B [X, Y, Z].
我成功地在2个方面实现了这个,忽略了第三个方面:

   /*A is projectile, B is static object*/
    //entity is object A
    //  - .v[3] is the speed vector
    //position[3] is array of coordinates of object B    


    double vector[3];                               //This is the vector c = A-B
    this->entityVector(-1, entity.id, vector);      //Fills the correct data
    double distance = vector_size(vector);          //This is distance |AB|
    double speed = vector_size(entity.v);      //This is size of speed vector a …
Run Code Online (Sandbox Code Playgroud)

c++ 3d game-physics

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

使用boost :: regexp忽略大小写

奇怪的是,谷歌拒绝回答这样一个简单的问题:
我如何使boost :: regexp不区分大小写?

这就是我所拥有的:

static const boost::regex bad_words("(?:^|.* )(f(?:uc|a)k(?:i[ng]{1,2})?|bitch(?:es|iz)?)(?:$| .*)");   //reduced to the english ones
Run Code Online (Sandbox Code Playgroud)

当然,我也希望过滤大写的坏词.这就是我匹配它们的方式:

//std::string ms; - chat messsage
//boost::match_results<std::string::const_iterator> results;  - prewious regexp results
else if(boost::regex_match(ms, results2, bad_words)) {   //
        std::stringstream msg;
        msg<<"Avoid bad words! Word '"<<results2[1]<<"' is banned!";
        this->whisper(results[1], msg.str());   //name, message
}
Run Code Online (Sandbox Code Playgroud)

那么,对于不敏感的正则表达式还有另一个函数吗?还是另一个正则表达式对象?或修改器i可用?

c++ case-insensitive boost-regex

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

将多个浮动div放在一起

我应该让3 div秒坐在一起.所以我做了一个div,我已经把三个divs,这个css样式:

div.holder div {
  float: left;
  width: 250px;
  background-color: yellow;   

  /*margin-right:auto;  /**These did not help**/
  margin-left:auto;  */
}
Run Code Online (Sandbox Code Playgroud)

和html这样:

<div class="holder">
  <div></div>
  <div></div>
  <div></div>
</div>
Run Code Online (Sandbox Code Playgroud)

它应该看起来像这样: 三个div 相反,它看起来像这样: 三个div

边框div应与灰线的末端对齐.

html css css-float centering

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

是否可以将传输的 ArrayBuffer 返回原始类型数组?

据我所知,将对象传输到 Web Worker 会导致主线程失去所有权。我想知道是否有什么办法可以让它重新获得所有权。这个Plunker(下面的代码)演示了我遇到的问题。

main.js

var worker = new Worker("worker.js");
var z = new Int16Array(10);
worker.onmessage = function(e) {
    console.log(e.data);  // [0, 1, ... 10]
    console.log(z);  // [], ownership not regained here
}
console.log(z);  // [0, 0, ... 0], original value here
worker.postMessage(z, [z.buffer]);
console.log(z);  // [], ownership lost here
Run Code Online (Sandbox Code Playgroud)

工人.js

self.onmessage = function(e) {
    var data = e.data;  // transferred "z" from main.js
    for (var i = 0; i < 10; i++) {
        data[i] = i;
    } …
Run Code Online (Sandbox Code Playgroud)

javascript multithreading web-worker

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

如何在 Unity 中正确创建无效的 Vector3?

我经常需要将无效位置传递给可以选择使用位置的函数(例如 RTS 单元操作)。但这不能编译:

public abstract void CastSpell(Spell spell, Vector3 targetLocation = null);
Run Code Online (Sandbox Code Playgroud)

那么我怎样才能制作一个正确的无效Vector3 来确定无效/“不关心”位置呢?我见过像带有 -999999 坐标的矢量这样的东西 - 但这是一个令人讨厌的技巧,可能会导致错误(例如在巨大的地图上)。

optional-parameters unity-game-engine

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

什么是正确的 Resource.Load 路径?

我正在尝试Texture2D使用 .png加载(.png) 资源Resource.Load。我试过以下路径模式:

Assets/CaseSensitivePath/TextureName
CaseSensitivePath/TextureName
Assets/CaseSensitivePath/TextureName.png
CaseSensitivePath/TextureName.png
Run Code Online (Sandbox Code Playgroud)

每次都Resource.Load(path, typeof(Texture2D))返回null。这是我的代码和错误处理:

public class LazyResource<T> where T : UnityEngine.Object
{

    //Path is read-only
    public string path { get { return _path; } }
    private string _path = "";
    //Whether NOT FOUND warning was thrown
    //in that case, further load attemts are ommited and the resource returns always null...
    public bool failed = false;
    //Constructor uses the path as first parameter
    public LazyResource(string path) {
        _path = path; …
Run Code Online (Sandbox Code Playgroud)

c# texture2d unity-game-engine

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