我正在写一个翻译器,为此,我想创建一个类动词。这个类应该只在不规则时保存完整的 cunjugation,因为它会产生大列表,占用大量内存。所以这是我的代码:
#include "string.h"
struct Irregular{
std::string present;
std::string simplepast;
std::string pastparticiple;
}
union Verbform{
Irregular* irregular;
std::string* regular;
Verbform(Irregular irreg){irregular=new Irregular(irreg);}
Verbform(std::string s){regular=new std::string(s);
~Verbbform(){delete regular;} //here is my problem
}
class Verb{
public:
//some public functions
private:
Verbform verbform;
//some other things;
}
Run Code Online (Sandbox Code Playgroud)
当我这样做并用一个不规则动词初始化它时,他是删除完整的不规则动词还是只删除第一个字符串?
当我这样做时: ~Verbform(){delete irregular;}并用普通字符串初始化它,他删除的内容是否超过了我希望他删除的内容?
在过去的几天里,我一直在阅读C#中的属性和方法之间的差异,以及何时使用它们.我读到的大多数文章/问题都说吸气剂应该是"轻巧的",并且内部永远不会有大量的逻辑或复杂的操作.
现在我有一个get我认为在属性和方法之间的界限,所以我想看看你们都在想什么,如果我应该改变方法或留在吸气剂.
还欢迎任何其他建议:D
public decimal[] getPreprocData
{
get
{
int i = 3;
decimal[] data = new decimal[9];
data[0] = (start.Value.Hour * 3600) + (start.Value.Minute * 60);
data[1] = duration.Value;
data[2] = flowRate.Value;
foreach (NumericUpDown nud in gbHTF.Controls.OfType<NumericUpDown>().OrderBy(nud => nud.TabIndex))
{
data[i] = nud.Value;
i++;
}
return data;
}
}
Run Code Online (Sandbox Code Playgroud) 那里!我是c ++的新手,遇到了删除指向数组的指针的问题.如果你搜索"如何正确删除指向数组的指针",之前有一个类似的问题.结论是,保持使用new和使用的习惯是一个好习惯delete.如果您使用new <data-type> []分配momery,delete[]应该遵循.
我对这个答案不满意.计算机在执行delete或delete[]执行时会做什么?所以,我试过这个.
#include <iostream>
using namespace std;
int main()
{
int* pvalue = NULL;
pvalue = new int[3];
cout << "address of pvalue:" << pvalue << endl;
for (int i = 0; i < 3; ++i)
{
*(pvalue+i) = i+1;
}
for (int i = 0; i < 3; ++i)
{
cout << "Value of pvalue : " << *(pvalue+i) << endl;
}
delete pvalue; …Run Code Online (Sandbox Code Playgroud) 我有一个 16 位数字,LSB 4 位用作检查设置的位域,MSB 12 位用作number递增的 a。我知道这tempNum = (data_bits >> 4)会让我number摆脱更大的。如果我想将其增加tempNum1,然后将其放回整个 16 位数字作为替换而不影响低 4 位,我将如何进行?我只想使用bitwise操作来做到这一点。
我正在编写一个名为的类StringSet,它具有vector<string> data和int length作为其私有成员。
bool StringSet::operator == (StringSet d)
{
for (int i = 0; i < length; i++)
{
if (data[i] == d.data[i])
{
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试像这样调用这个函数时,
StringSet doc1, doc2;
if (doc1 == doc2)
{
cout << "Both sentences are identical!\n";
}
Run Code Online (Sandbox Code Playgroud)
我得到一个断言失败,说向量下标超出范围,我知道这意味着什么,但我不知道它在这里意味着什么。如果有人能指出我犯的一个明显错误,那就太好了,因为我是 C++ 的新手。