我刚刚开始进入C++,我想养成一些好习惯.如果我刚刚int使用new运算符分配了一个类型的数组,我怎么能将它们全部初始化为0而不用自己循环遍历它们?我应该用memset吗?有没有"C++"方法呢?
在浏览一些源代码时,我遇到了这样的函数:
void someFunction(char someArray[static 100])
{
// do something cool here
}
Run Code Online (Sandbox Code Playgroud)
通过一些实验,似乎其他限定符也可能出现在那里:
void someFunction(char someArray[const])
{
// do something cool here
}
Run Code Online (Sandbox Code Playgroud)
似乎只有[ ]在将数组声明为函数的参数时才允许使用限定符.这些怎么办?为什么功能参数不同?
我正在尝试进行一些数字舍入和转换为字符串以增强Objective-C程序中的输出.
我有一个浮点值,我想要舍入到最近的.5然后用它来设置标签上的文本.
例如:
1.4将是一串:1.5
1.2将是一串:1
0.2将是一个字符串:0
我花了一段时间在Google上寻找答案但是,作为Objective-C的菜鸟,我不知道该搜索什么!所以,我真的很感激指向正确的方向!
谢谢,阿什
我想为了记录调用而覆盖对各种API的某些函数调用,但我也可能希望在将数据发送到实际函数之前对其进行操作.
例如,假设我getObjectName在源代码中使用了一个名为数千次的函数.我想暂时覆盖此函数,因为我想更改此函数的行为以查看不同的结果.
我创建了一个像这样的新源文件:
#include <apiheader.h>
const char *getObjectName (object *anObject)
{
if (anObject == NULL)
return "(null)";
else
return "name should be here";
}
Run Code Online (Sandbox Code Playgroud)
我像往常一样编译所有其他源代码,但是在链接API的库之前我首先将它链接到此函数.这工作正常,但我显然不能在我的重写函数中调用真正的函数.
是否有一种更简单的方法来"覆盖"一个函数而不会链接/编译错误/警告?理想情况下,我希望能够通过编译和链接一个或多个额外的文件来覆盖该函数,而不是通过链接选项或改变我的程序的实际源代码.
我发现很多时候,OpenGL会通过不绘制任何东西来告诉你失败.我正试图通过检查转换矩阵堆栈等来找到调试OpenGL程序的方法.调试OpenGL的最佳方法是什么?如果代码外观和感觉顶点位于正确的位置,您如何确定它们是什么?
在Objective-C中,您可以使用以下命令调用类方法:
[MyClass aClassMethod];
Run Code Online (Sandbox Code Playgroud)
您可以使用以下命令查询实例的类型:
[someInstance isKindOfClass:[MyClass class]];
Run Code Online (Sandbox Code Playgroud)
但是,为什么我们需要做[MyClass class],而不是简单地提供MyClass这样的:
[someInstance isKindOfClass:MyClass];
Run Code Online (Sandbox Code Playgroud)
有没有理由认为编译器遇到MyClass接收器(指针类型)而不是作为参数?这是解析语言的限制吗?或者也许是编译器的限制?
我正在编写一个函数来确定字符串是否只包含字母数字字符和空格.我正在有效地测试它是否与正则表达式匹配^[[:alnum:] ]+$但不使用正则表达式.这是我到目前为止:
#include <algorithm>
static inline bool is_not_alnum_space(char c)
{
return !(isalpha(c) || isdigit(c) || (c == ' '));
}
bool string_is_valid(const std::string &str)
{
return find_if(str.begin(), str.end(), is_not_alnum_space) == str.end();
}
Run Code Online (Sandbox Code Playgroud)
是否有更好的解决方案或"更多C++"方式来做到这一点?
当我显示这样的NSAlert时,我立刻得到了回复:
int response;
NSAlert *alert = [NSAlert alertWithMessageText:... ...];
response = [alert runModal];
Run Code Online (Sandbox Code Playgroud)
问题是这是应用程序模式,我的应用程序是基于文档的.我使用工作表在当前文档的窗口中显示警报,如下所示:
int response;
NSAlert *alert = [NSAlert alertWithMessageText:... ...];
[alert beginSheetModalForWindow:aWindow
modalDelegate:self
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
contextInfo:&response];
//elsewhere
- (void) alertDidEnd:(NSAlert *) alert returnCode:(int) returnCode contextInfo:(int *) contextInfo
{
*contextInfo = returnCode;
}
Run Code Online (Sandbox Code Playgroud)
唯一的问题是beginSheetModalForWindow:直接返回,所以我无法可靠地询问用户问题并等待回复.如果我可以将任务分成两个区域,那么这不会是一件大事,但我不能.
我有一个循环来处理大约40个不同的对象(在树中).如果一个对象出现故障,我希望警报显示并询问用户是继续还是中止(继续在当前分支处理),但由于我的应用程序是基于文档的,因此Apple人机界面指南要求在警报为时使用工作表特定于文件.
如何显示警报表并等待响应?
class Temp
{
private:
~Temp() {}
friend class Final;
};
class Final : virtual public Temp
{
public:
void fun()
{
cout<<"In base";
}
};
class Derived : public Final
{
};
void main()
{
Derived obj;
obj.fun();
}
Run Code Online (Sandbox Code Playgroud)
上面的代码试图实现不可继承的类(final).但是使用上面的代码仍然可以创建派生的对象,为什么呢?
只有当ctor私有化时才能实现所需的功能,我的问题是为什么在dtor私有的情况下无法实现?
在C++标准库中,std::string有一个公共成员函数capacity(),它返回内部分配存储的大小,一个大于或等于字符串中字符数的值(根据此处).这个值可以用于什么?它与自定义分配器有关吗?
c++ ×4
c ×2
cocoa ×2
macos ×2
objective-c ×2
string ×2
alerts ×1
allocator ×1
arrays ×1
capacity ×1
debugging ×1
final ×1
formatting ×1
function ×1
inheritance ×1
iterator ×1
linker ×1
new-operator ×1
opengl ×1
overriding ×1
parameters ×1
rounding ×1
static ×1