据我所知,最好使用智能指针,而不是通过原始指针管理动态分配对象的生命周期,例如: MyObject* obj = new Object();
但是在某些框架/库中,它们总是返回/使用原始指针而不是智能指针(也许它们有自己的GC对象?我不知道).
它也更容易使用
MyObject* obj = GetAObject(); // return raw owning pointer
Run Code Online (Sandbox Code Playgroud)
比
SharedPointer<MyObject> obj = GetAObject(); // return smart pointer
Run Code Online (Sandbox Code Playgroud)
应该总是使用智能指针而不是手动new/ delete(如上例所示),还是应该使用原始资源拥有指针?
我正在尝试做一个基本的if语句,我的字符串出现了这个奇怪的错误.
错误1:
指针和整数之间的比较('int'和'char*')
错误2:
未指定与字符串文字进行比较的结果(改为使用strncmp)
这是函数的副本全部发生在.
int logOn(int *par)
{
char p;
printf("Log into student records system\nEnter password or type cancel to leave\n>");
scanf("%s", &p);
if(p == *PASSWORD1 | p == *PASSWORD2 | p == *PASSWORD3)
{
*par = 2;
}
else if (p = "cancel")
{
*par = 3;
}
else
{
printf("\nIncorrect password try again\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
else if语句行(p = "cancel")上发生错误.
我正在将一个C++项目转换为Java.我有一个疑问,char**在Java中是等同于什么,是它String[]还是String[][]?
我写了一个简单的C++程序来测试C++中默认复制行为的概念.所以,我有一个示例类X,我创建了一个名为"var"的X类对象,然后我尝试使用"var"初始化另一个名为"defaultCopyObject"的对象,假设默认复制行为只是一个对象的成员副本到另一个.我已正确理解了这个概念,但我无法理解用于初始化的语法.
X defaultCopyObject{var};// this code snippet works perfectly fine
// this code snippet returns a compile time error
X defaultCopyObject;
defaultCopyObject{var};
Run Code Online (Sandbox Code Playgroud)
有人可以解释我为什么前一个声明有效而后一个无效?
任何人都可以解释,为什么大小objC是8,而不是12?
我认为,如果在课堂上C我有对象类型int和A,B- char,然后创作之后objC它应该是4个字节objA,objB和ObjC.
class A {
char a;
};
class B : A {
char b;
};
class C : B {
int c;
};
int main()
{
A objA;
B objB;
C objC;
cout << sizeof(objA) << endl; // 1
cout << sizeof(objB) << endl; // 2
cout << sizeof(objC) << endl; // 8! Why not 12?
return 0;
} …Run Code Online (Sandbox Code Playgroud) 我有这个问题很长一段时间.我试图想象3门问题,只是为了乐趣和练习Swift.所以我有:
3扇门,因此所有门都有3种不同的IBAction和3种功能.这些功能都完全相同,但每个代码中只有门的数量不同.所以我想知道,我可以缩短这段代码吗?:
func openSecondChoice(whatDoorIsClickedOn: Int)
{
if whatDoorIsClickedOn == 1
{
if whatDoorIsClickedOn == doorWithNumber
{
UIButtonDoor1.setBackgroundImage( UIImage (named: "doorWithMoney"), for: UIControlState.normal)
}
else
{
UIButtonDoor1.setBackgroundImage( UIImage (named: "doorWithGoat"), for: UIControlState.normal)
}
}
if whatDoorIsClickedOn == 2
{
if whatDoorIsClickedOn == doorWithNumber
{
UIButtonDoor2.setBackgroundImage( UIImage (named: "doorWithMoney"), for: UIControlState.normal)
}
else
{
UIButtonDoor2.setBackgroundImage( UIImage (named: "doorWithGoat"), for: UIControlState.normal)
}
}
if whatDoorIsClickedOn == 3
{
if whatDoorIsClickedOn == doorWithNumber
{
UIButtonDoor3.setBackgroundImage( UIImage (named: "doorWithMoney"), for: UIControlState.normal)
}
else
{
UIButtonDoor3.setBackgroundImage( …Run Code Online (Sandbox Code Playgroud) 这就是我所拥有的:
struct menuitem {
std::string Text;
// …
};
std::vector<menuitem> ItemList;
// populate ItemList…
std::vector<size_t> TextSizeList;
Run Code Online (Sandbox Code Playgroud)
现在我需要在ItemList中填写TextSizeList每个Text的长度menuitem...
我能做到这一点:
TextSizeList.reserve(ItemList.size());
for (const item& Item : ItemList)
TextSizeList.push_back(Item.Text.size());
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有一个std::功能来完成这个,类似std::generate但是带有Generator参数,还有一个额外的OutputIterator?沿着:
void std::foo(InputIterator first, InputIterator last, OutputIterator out, Generator gen);
Run Code Online (Sandbox Code Playgroud)
然后可以像这样使用:
std::foo(ItemList.begin(), ItemList.end(), TextSizeList.begin(),
[](const menuitem& Item) { return Item.Text.size(); } );
Run Code Online (Sandbox Code Playgroud) 请参阅以下代码
#include <stdio.h>
#include<stdlib.h>
#define MALLOC(n) malloc(n)
int main(void) {
int *x = MALLOC (sizeof(int) * 10);
x[0] = 20;x[9] =200;
printf("%d %d\n",x[0],x[9]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我应该打电话给MALLOC(sizeof(int) * 10)对吗?由于宏应该与调用匹配.即使我打电话给MALLOC (sizeof(int) * 10)我得到相同的输出是这个未定义的行为?或者我错过了什么?
在后MALLOC一个调用之后的空格是否被忽略了?
标准说当我们调用一个宏时,它的确切文本表示被替换不是这里的情况吗?
在这里,我试图将一串数字转换为相应的双数字.
double string_to_double ( const char* str)
{
int i = 0;
double* num = (double *)malloc(sizeof(double));
*num = 0;
int fract_fact = 10;
int size = strlen(str);
if ( str[0] != '.') // if its not starting with a point then take out the first decimal digit and place it in the number instead of 0.
*num = (str[i] - '0');
for ( i = 1; i < size; ++i){
if ( str[i] == '.'){
i++;
for (int j …Run Code Online (Sandbox Code Playgroud) struct departure_compare {
bool operator() (const Leg* lhs, const Leg* rhs) const
{
return lhs->CurrentDepartureTime() < rhs->CurrentDepartureTime();
}
};
class Station
{
uint station_number_;
std::set<Leg *, departure_compare> departure_legs_in_order_; // legs that depart from this station in order of departure time
public:
Station(uint station_number) : station_number_(station_number) {};
void addDepartureLeg(Leg *leg) { departure_legs_in_order_.insert(leg); };
const std::set<Leg *, departure_compare>& DepartureLegs() const { return departure_legs_in_order_; };
uint StationNumber() { return station_number_; };
};
Run Code Online (Sandbox Code Playgroud)
我把它称为循环
Leg *new_leg = new Leg();
start_station->addDepartureLeg(new_leg); // start_station of type …Run Code Online (Sandbox Code Playgroud)