我的崇高任务是摆脱单身人士和静态阶级.
背景:
我有以下结构:
问题:
问题是我试图摆脱所有的静态类(我现在变成了用于测试的单例),并且我使系统完全模块化并且松散耦合.这反过来阻止我进行Cmds可能指向的静态调用.
第一直觉是将函数从typedef更改为模板类,这将存储一个对象和方法,但它看起来非常混乱和复杂,我个人不舒服:
Cmd::create("toggleconsole", Console::toggle);
Run Code Online (Sandbox Code Playgroud)
至:
Cmd::create("toggleconsole", new FunctorObject<Console>(&Console::get(), &Console::toggle));
Run Code Online (Sandbox Code Playgroud)
最终的Cmd创建看起来非常模糊和误导谁负责Functor解除分配.
我也正在将Cmd创建从静态方法调用移动到Commander类中,所以它看起来像commander.createCmd("command_name",...); 而不是Cmd :: create("command_name",...); 这是因为Commander不再是静态的(或单例),因此它处理的所有命令都必须属于它.
但是,我完全不知道我的选项/替代方案是注册Cmds,并通过允许向Commander发出字符串命令来维持松散耦合.
我已经考虑过让每个主类派生自一个CmdListener类,它会在创建时用Commander注册该对象,然后在执行期间将命令传递给覆盖"onCmd(const Cmd&command)"的所有已注册对象.
这也留下了一些未解答的问题:Cmd将如何调用应该调用哪个类的方法?保持指针是没有意义的,并且会受到高度默默无闻的影响(如上所示).另外,我希望不为每个可以处理该cmd的类重新解释onCmd方法中的字符串.
这是很多信息,但有没有人对如何处理这个问题有任何想法?
此外,我的所有类都必须知道Commander和Console对象,它们不再是单例/静态.到目前为止,我已将它们放在Context对象中,并将它像一个小胶囊一样传递.关于如何解决这些后单例残留问题的任何想法?
这个项目是我个人的工作,我打算在我的简历上使用它 - 因此,我不希望我的潜在雇主看到任何单身人士(我也不想解释为什么,因为我可以证明他们自己并非真正必要).
万分感谢!
编辑:排版.
我有一个Structure(在这种情况下是一个Netlink消息头),我需要通过套接字发送到内核.我弄清楚的唯一方法是使用__reduce__().
>>> class nlmsghdr(ctypes.Structure):
... _fields_ = [('nlmsg_len', ctypes.c_int32),
... ('nlmsg_type', ctypes.c_int16),
... ('nlmsg_flags', ctypes.c_int16),
... ('nlmsg_seq', ctypes.c_int32),
... ('nlmsg_pid', ctypes.c_int32)]
...
>>>
>>> hdr = nlmsghdr(20, 22, 769, 1328884876, 0)
>>> hdr.__reduce__()[1][1][1]
'\x14\x00\x00\x00\x16\x00\x01\x03\x8c,5O\x00\x00\x00\x00'
>>> # socket.send(hdr.__reduce__()[1][1][1])
Run Code Online (Sandbox Code Playgroud)
它看起来像是__reduce__序列化(pickle)并依赖于它总是以相同的方式运行似乎是一个错误.
肯定有更好的办法?
在提到malloc调用的结构大小时,最好提一下结构的名称或解引用的结构指针变量吗?
例:
struct ExampleStruct
{
int x;
char arr[40];
};
int main()
{
struct ExampleStruct *Y = NULL;
Y = malloc(sizeof(struct ExampleStruct)); //Is this better?
Y = malloc(sizeof(*Y)); //Is this better?
}
Run Code Online (Sandbox Code Playgroud)
我个人更喜欢,sizeof(struct ExampleStruct)因为我看到许多开发人员在第二种方法中错误地错过了'*',即他们错误地输入它malloc(sizeof(Y)),在这种情况下,分配的内存将只有4个字节.但是,我已经看到它也被普遍使用.
考虑以下C#:
// C# .net
switch(x)
{
case 1:
for(int i = 0; i < 10; i++)
{
int val = getValue(i);
if (val == 0)
goto endswitch;
}
doMoreStuff();
break;
case 2:
doSomeThingElse();
break;
default: throw new ArgumentOutOfRangeException();
}
endswitch: ;
Run Code Online (Sandbox Code Playgroud)
我编写了类似于上面代码示例的代码.问题是我需要从内部for循环内部中断switch语句.如果我在break那里放一个语句,它只会打破内部for循环,然后继续doMoreStuff(),这不是我需要的.
这里似乎最有效的替代方案是goto声明,但我知道这是不赞成的.
另一种方法是在for循环中跟踪一个单独的变量,但是这会增加代码行并且不那么重要.
做这个的最好方式是什么?
更新:我已经读过有一种方法可以在JavaScript中执行此操作.它的工作方式如下:(http://www.devguru.com/technologies/ecmascript/quickref/break.html)
// JavaScript
outer_loop:
for(i=0; i<3; i++)
{
document.write("<BR>" + "outer " + i + ": ");
for(j=0; j<5; j++)
{
document.write("inner " + j + " "); …Run Code Online (Sandbox Code Playgroud) 几乎每次我跳回C项目时,我都会这么说.在尝试访问结构中的结构时,我遇到了段错误.假设我有一个游戏的以下(简化)结构:
struct vector {
float x;
float y;
};
struct ship {
struct vector *position;
};
struct game {
struct ship *ship;
} game;
Run Code Online (Sandbox Code Playgroud)
以及初始化船舶的功能:
static void
create_ship(struct ship *ship)
{
ship = malloc(sizeof(struct ship));
ship->position = malloc(sizeof(struct vector));
ship->position->x = 10.0;
}
Run Code Online (Sandbox Code Playgroud)
然后在main()中:
int main() {
create_ship(game.ship);
printf("%f\n", game.ship->position->x); // <-- SEGFAULT
}
Run Code Online (Sandbox Code Playgroud) struct books
{
char name[100];
float price;
int pages;
};
Run Code Online (Sandbox Code Playgroud)
声明一个结构而不产生object的structure,莫非是结构占用内存空间为它的DATA MEMBERS?
以链接列表定义的结构为例...
struct test_struct line 1
{ line 2
int val; line 3
struct test_struct *next; line 4
}; line 5
Run Code Online (Sandbox Code Playgroud)
在第4行,由于test_struct甚至没有完全定义(我假设结构在第5行完全定义,因为';',之前我们不能说结构是定义的)那么为什么我们不会在第4行得到错误test_struct没有定义......?
我已经宣布了这样的结构 - >
struct data{
int x,y;
bool operator < (const data& other) {
return x<other.x or y<other.y;
}
};
Run Code Online (Sandbox Code Playgroud)
现在我希望map它作为一个关键并具有bool价值.
int main()
{
data a;
map<data,bool>mp;
a.x=12, a.y=24;
mp[a]=true;
}
Run Code Online (Sandbox Code Playgroud)
最后一行给了我这个错误 - >
error: passing 'const' as 'this' argument of 'bool data::operator<(const data&)' discards qualifiers
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题 ??
我用C++开发了Insertion Sort和Quicksort算法.现在,我打算创建至少四种Quicksort算法的变体.它们在选择数据透视的方式以及是否对小列表使用插入排序方面会有所不同.在Java或C#中,为避免代码重复和名称冲突,我将在单独的类文件中实现每个版本的Quicksort算法并使用继承.具体来说,我会创建以下类:
QuicksortFixedPivotQuicksortRandomPivotQuicksortFixedPivotInsertion- k使用插入排序对包含大多数元素的子数组进行排序QuicksortRandomPivotInsertion但是,根据我的理解,像Quicksort这样的"独立"算法通常不会在C++的类中实现.
下面是我最初的Quicksort实现.
quicksort.hpp
#pragma once
#include <vector>
namespace algorithms
{
void quicksort(std::vector<int> &list);
void quicksort(std::vector<int> &list, int left, int right);
int partition(std::vector<int> &list, int left, int right);
}
Run Code Online (Sandbox Code Playgroud)
quicksort.cpp
#include "quicksort.hpp"
namespace alg = algorithms;
void alg::quicksort(std::vector<int> &list)
{
alg::quicksort(list, 0, list.size() - 1);
}
void alg::quicksort(std::vector<int> &list, int left, int right)
{
if (left >= right)
return;
int oldPivot = alg::partition(list, left, right);
alg::quicksort(list, left, oldPivot - 1);
alg::quicksort(list, oldPivot …Run Code Online (Sandbox Code Playgroud) 我有以下代码(简化)、一个结构和一个类。
public struct pBook
{
private int testID;
public string request;
public string response;
public Int32 status;
public int test_id
{
get
{
return testID;
}
set
{
testID = value;
}
}
};
public class TestClass
{
public static void Main(string[] args)
{
pBook Book1;
pBook Book2;
Book1.request = "a";
Book2.response = "b";
Book2.status = 201;
Book2.test_id = 0; //this doesn't work, why?
}
}
Run Code Online (Sandbox Code Playgroud)
在声明中
Book2.test_id = 0;
Run Code Online (Sandbox Code Playgroud)
我收到错误
使用未分配的局部变量“Book2”
任何想法如何纠正?