我想问一些关于正确销毁int指针和向量指针的快速问题.首先,我看到人们过去曾经问过这些类型的问题,并且几乎总是有几个关于如何在C++中使用向量指针,指向对象的指针等不是很好的标准c ++编码实践的回答,并且你应该实例化一个对象的副本.这可能是真的,但你并不总能掌控到达之前已经铺设的范例.我需要工作的范例需要初始化指向几乎所有内容的指针.一种非常类似Java的C++方法.我们这样做的主要原因之一是我们的数据集非常大,堆栈分配会受到溢出的影响.
我的问题:
如果我有一个指向int32_t数组的指针,那么在析构函数中销毁它的正确方法是什么?
注意:我们的做法是在构造函数中将任何指针设置为NULL.
I initialize it as a member variable.
int32_t *myArray_;
When I use it in a method, I would:
this->myArray = new int32_t[50];
To delete it in the method I would call delete on the array:
delete [] this->myArray;
What is the proper call in the destructor?
~MyDestructor(){
delete this->myArray_;
or delete [] this->myArray_;
}
Run Code Online (Sandbox Code Playgroud)
我对矢量指针有同样的问题:
I initialize it as a member variable.
std::vector<MyObject*> *myVector_;
When I use it in a method, I would:
this->myVector_ = …
Run Code Online (Sandbox Code Playgroud) 我对JQuery很新,但有人告诉我,关于它的一个很酷的事情就是你可以直接从一个html页面(或者在我的情况下是一个聪明的模板)查询一个mysql数据库而不需要php.
我没有找到任何这方面的例子,所以我问是否有人?谢谢
我需要执行的操作要求我从char数组中获取一个int32_t值和2个int64_t值
char数组的前4个字节包含int32值,接下来的8个字节包含第一个int64_t值,接下来的8个字节包含第二个字节.我无法弄清楚如何获得这些价值观.我试过了;
int32_t firstValue = (int32_t)charArray[0];
int64_t firstValue = (int64_t)charArray[1];
int64_t firstValue = (int64_t)charArray[3];
int32_t *firstArray = reinterpet_cast<int32_t*>(charArray);
int32_t num = firstArray[0];
int64_t *secondArray = reinterpet_cast<int64_t*>(charArray);
int64_t secondNum = secondArray[0];
Run Code Online (Sandbox Code Playgroud)
我只是抓住稻草.任何帮助赞赏
我已经使用了这个简单的通用方法来实现它,它适用于基于应用程序的对话框,但是我希望在工作表样式对话框中使用相同的功能,并且我很难将它们放在一起.
根据我理解的文档,OS10.9及更高版本中唯一不被弃用的方法是将NSAlert类与完成处理程序进程一起使用.这似乎使得从通用方法返回Bool几乎是不可能的.
我的代码:
-(BOOL)confirm :(NSString*)questionTitle withMoreInfo:(NSString*)addInfo andTheActionButtonTitle:(NSString*)actionType{
BOOL confirmFlag = NO;
NSAlert *alert = [NSAlert alertWithMessageText: questionTitle
defaultButton:actionType
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@"%@",addInfo];
[alert setAlertStyle:1];
NSInteger button = [alert runModal];
if(button == NSAlertDefaultReturn){
confirmFlag = YES;
}else{
confirmFlag = NO;
}
return confirmFlag;
}
The [alert runModal] returns the value I can return.
Run Code Online (Sandbox Code Playgroud)
使用较新的范例,[alert beginSheetModalForWindow:[self window] sheetWindow completionHandler:some_handler]不允许我更新或返回方法结尾的值.我知道为什么,但有没有办法我不想做到这一点.
请告诉我如何创建一个类似于我用于工作表的方法.
谢谢三重
我有一个弹出按钮,在编程上加载了5个选项,如果选择了一个特定的选项,让我们说"追加文件名末尾",那么我的更新功能需要一个带有textField的小弹出窗口和一个保存并取消按钮显示.我不知道该怎么做.我能够得到一个很好的NSAlert示例,但它不需要我知道的textField.是否有一个对话类或我应该使用的其他模态,或者我应该尝试创建第二个笔尖?在任何一种情况下我都不知道该怎么做,所以一个好的例子或教程会很棒.
谢谢
有人可以解释使用#import语句包含额外的头文件和使用@class之间的区别.例如
//MyClass.h
#import <Foundation/foundation.h>
#import "someOtherClass.h"
Run Code Online (Sandbox Code Playgroud)
要么
//MyClass.m
#import"MyClass.h"
@class someOtherClass
implementation
Run Code Online (Sandbox Code Playgroud)
当我尝试使用第二种方法时,它并不总是有效.
如果相同的概念适用于同一场景的C++,我现在也想
谢谢
我在Mac OSX Lion上使用C++,我创建了以下代码:
float* floatArray = new float[10];
for(int i = 0; i < 10; i++)
{
floatArray[i] = 0.0 ;
}
std::cout<< "Test size of a float " << sizeof(floatArray[0]) << std::endl;
// The value is 4 byte which is what I would expect.
std::cout<< "Test the size of the whole array" << sizeof(floatArray) <<std::endl;
// the value is 8. I would have expected the value to be 40 bytes.
Run Code Online (Sandbox Code Playgroud)
我不明白的是什么?
提前致谢
好的,我放弃了并寻求帮助;
我有一个脚本来访问MySQL数据库中的信息.我希望我的凭据隐藏在我可以包含的文件中以确保安全.为了使它更整洁,我试图在我的cgi-bin目录中创建一个子目录(include).
这是一个省略所有数据库内容的简单示例(因为这不是我的问题).
#!/usr/bin/perl
use strict;
use warnings;
$data_base = 'dbi::mysql::test_db';
$db_user = 'some_user';
$db_pw = 'password';
Run Code Online (Sandbox Code Playgroud)
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
require'/include/config.pl';
$param1 = $data_base;
$param2 = $db_user;
$param3 = $db_pw;
Run Code Online (Sandbox Code Playgroud)
我得到的第一个错误是找不到包含.我还看到许多帖子显示our
在vars前面添加.大多数帖子都提到不使用require
,但对于简单的事情,我宁愿.
我最后学习了一些关于如何制作模块的教程.我得到了它的工作,但这似乎是一个简单的配置文件的很多努力.
我想了解一些如何处理isstringstream对象的帮助.
我试图标记文件的每一行,以便在检查标记中的某些数据值后,我可以用另一种格式重写它.我在tokenVector中加载每一行并迭代向量.我的代码有效,但我关心的是我必须为每次迭代实例化一个isstringstrem对象,否则它不起作用.这感觉不对.她是我的代码:
std::string line;//each file line
std::ifstream myFile (info.txt.c_str());
if(myFile.is_open()){
getline(myFile, line);
std::vector<std::string> tokenVector;
//create a isstringstream object for tokenizing each line of the file
std::istringstream hasTokens(line);
while(hasTokens)
{
std::string substring;
if(! getline(hasTokens, substring,','))
break;
tokenVector.push_back(substring);
}
//look for some known header names for validation
if(!tokenVector.empty()){
if(!(tokenVector[0]=="Time")&&(tokenVector[1] == "Group")&&(tokenVector[2]=="Perception")&&(tokenVector[3] == "Sign")){
setErrorMesssage("Invalid Header in myFile");
return false;
}
tokenVector.clear();
}
//clear the isstringstream object
hasTokens.str(std::string());
//if header validates, do rest of file
while(myFile.good()){
getline(myFile , line);
//break line into tokens …
Run Code Online (Sandbox Code Playgroud) 关于将结构作为函数arg传递的快速问题.也许它是C的延续,在C++中没有任何区别.
在许多Linked List示例中,您看到它们在任何带有结构的函数参数中重新声明了单词struct,我不明白为什么.或任何分配.结构是它自己的对象,只声明结构的名称就足够了.
例:
struct Node{
int data;
Node * next;
};
// Function to output data.
void printNode(struct Node* myNode){
// Print data of node
}
Run Code Online (Sandbox Code Playgroud)
为什么在函数arg中重新声明了struct这个词.声明类型Node*工作得很好.
谢谢.