c++

Is there any library/tool for formal specification in C++, such as JML for Java, and Data Contracts in C#? EDIT: I am not looking for something more specific than, it's practical to use and adds some value/quality. EDIT2: I am not looking for UML tools, please see the examples I have provided. IBM Rational Rose UML software generates C++ code according to the complete UML model. N

C ++

C ++中是否有正式规范的库/工具,如JML for Java和C#中的数据合同? 编辑: 我不是在寻找更具体的东西,而是使用并增加一些价值/质量。 EDIT2: 我不在寻找UML工具,请参阅我提供的示例。 根据完整的UML模型,IBM Rational Rose UML软件生成C ++代码。 从来没有使用它,但不是我的意思。 只是一个简短的看看,只在UML功能。 看看Larch / C ++:Larch / C ++参考手册 frama-c - 引人入胜,jml灵感来源,我一直试

What is the difference between const int*, const int * const, and int const *?

I always mess up how to use const int* , const int * const , and int const * correctly. Is there a set of rules defining what you can and cannot do? I want to know all the do's and all don'ts in terms of assignments, passing to the functions, etc. Read it backwards (as driven by Clockwise/Spiral Rule): int* - pointer to int int const * - pointer to const int int * const - const

const int *,const int * const和int const *之间有什么区别?

我总是搞砸了如何正确使用const int* , const int * const和int const * 。 是否有一套规则来定义你可以做什么和不可以做什么? 我想知道所有的做法和所有的要点,包括任务,传递给功能等。 向后读取(由顺时针/螺旋规则驱动): int* - 指向int的指针 int const * - 指向const int的指针 int * const - const指向int的指针 int const * const - const int指针 现在第一个const可以在类型的任一侧,如下所示:

Easiest way to convert int to string in C++

What is the easiest way to convert from int to equivalent string in C++. I am aware of two methods. Is there any easier way? (1) int a = 10; char *intStr = itoa(a); string str = string(intStr); (2) int a = 10; stringstream ss; ss << a; string str = ss.str(); C++11 introduces std::stoi (and variants for each numeric type) and std::to_string , the counterparts of the C atoi and itoa b

将int转换为C ++字符串的最简单方法

从int转换为C ++中的等效string的最简单方法是什么? 我知道两种方法。 有没有更简单的方法? (1) int a = 10; char *intStr = itoa(a); string str = string(intStr); (2) int a = 10; stringstream ss; ss << a; string str = ss.str(); C ++ 11引入了std::stoi (以及每种数字类型的变体)和std::to_string ,C atoi和itoa的对应部分,但用std::string 。 #include <string> std::string s = std::

When to use references vs. pointers

I understand the syntax and general semantics of pointers versus references, but how should I decide when it is more-or-less appropriate to use references or pointers in an API? Naturally some situations need one or the other ( operator++ needs a reference argument), but in general I'm finding I prefer to use pointers (and const pointers) as the syntax is clear that the variables are being

何时使用引用与指针

我理解指针与引用的语法和一般语义,但是我应该如何决定何时适合在API中使用引用或指针? 当然,有些情况需要一个或另一个( operator++需要一个引用参数),但通常我发现我更喜欢使用指针(和指针),因为语法清楚地表明变量正在被破坏性地传递。 例如在下面的代码中: void add_one(int& n) { n += 1; } void add_one(int* const n) { *n += 1; } int main() { int a = 0; add_one(a); // Not clear that a may be

How to pass objects to functions in C++?

I am new to C++ programming, but I have experience in Java. I need guidance on how to pass objects to functions in C++. Do I need to pass pointers, references, or non-pointer and non-reference values? I remember in Java there are no such issues since we pass just the variable that holds reference to the objects. It would be great if you could also explain where to use each of those options.

如何将对象传递给C ++中的函数?

我是C ++编程新手,但我有Java经验。 我需要指导如何将对象传递给C ++中的函数。 我是否需要传递指针,引用或非指针和非引用值? 我记得在Java中没有这样的问题,因为我们只传递了保存对象引用的变量。 如果你还可以解释在哪里使用这些选项,那将是非常好的。 经验法则C ++ 11: 按价值传递,除了何时 你不需要拥有该对象的所有权,并且一个简单的别名会做,在这种情况下,你通过const引用传递 , 你必须改变对象

What is a smart pointer and when should I use one?

什么是智能指针,我应该什么时候使用它? A smart pointer is a class that wraps a 'raw' (or 'bare') C++ pointer, to manage the lifetime of the object being pointed to. There is no single smart pointer type, but all of them try to abstract a raw pointer in a practical way. Smart pointers should be preferred over raw pointers. If you feel you need to use pointers (first consider if

什么是智能指针,我应该什么时候使用它?

什么是智能指针,我应该什么时候使用它? 智能指针是一个包装“裸”(或“裸”)C ++指针的类,用于管理指向的对象的生命周期。 没有单一的智能指针类型,但他们都尝试以实用的方式提取原始指针。 智能指针应该优于原始指针。 如果你觉得你需要使用指针(首先考虑你是否真的这么做),你通常会使用智能指针,因为这可以减轻原指针的许多问题,主要是忘记删除对象和泄漏内存。 使用原始指针时,程序员必须在不再有用时明确销毁

Are memory leaks ever ok?

Is it ever acceptable to have a memory leak in your C or C++ application? What if you allocate some memory and use it until the very last line of code in your application (for example, a global object's destructor)? As long as the memory consumption doesn't grow over time, is it OK to trust the OS to free your memory for you when your application terminates (on Windows, Mac, and Linux)

内存泄漏是否正常?

在C或C ++应用程序中发生内存泄漏是否可以接受? 如果你分配了一些内存并使用它直到应用程序中最后一行代码(例如全局对象的析构函数)呢? 只要内存消耗不会随着时间增长,在应用程序终止时(Windows,Mac和Linux),信任操作系统为您释放内存是否可行? 如果内存被连续使用直到被操作系统释放,你会否认为这是真正的内存泄漏? 如果第三方图书馆强迫你这样做,会怎么样? 不管它有多么伟大,它会拒绝使用第三方库吗?

How do I tokenize a string in C++?

Java has a convenient split method: String str = "The quick brown fox"; String[] results = str.split(" "); Is there an easy way to do this in C++? Your simple case can easily be built using the std::string::find method. However, take a look at Boost.Tokenizer. It's great. Boost generally has some very cool string tools. The Boost tokenizer class can make this sort of thing quite simp

如何在C ++中标记字符串?

Java有一个方便的拆分方法: String str = "The quick brown fox"; String[] results = str.split(" "); 有没有简单的方法来在C ++中做到这一点? 您的简单案例可以使用std::string::find方法轻松构建。 但是,请看一下Boost.Tokenizer。 这很棒。 Boost通常有一些非常酷的字符串工具。 Boost tokenizer类可以使这种事情变得非常简单: #include <iostream> #include <string> #include <boost/foreach.

Why doesn't C++ support dynamic arrays on the stack?

In C99 this was legal: void f(size_t sz) { char arr[sz]; // ... } However, this - dynamically sized stack arrays - has been dropped in C++, and not seeing a return in C++11. AFAIK C++ was made with C compatibility in mind, so I wondered There must be some very good argument of not including this useful feature, right? All I could think of was this: Pros Memory savings by allowin

为什么C ++不支持堆栈上的动态数组?

在C99中这是合法的: void f(size_t sz) { char arr[sz]; // ... } 但是,这个动态调整大小的堆栈数组已经被C ++放弃了,并且没有在C ++ 11中看到返回结果。 AFAIK C ++是考虑到C兼容性的,所以我想知道一定有一些非常好的论点,不包括这个有用的功能,对吧? 我能想到的只有这个: 优点 通过允许需要在堆栈上的更智能的阵列大小(临时缓冲区?)来节省内存。 较少的“智能指针”(或更糟糕的是,引入delete [

When should I use the new keyword in C++?

I've been using C++ for a short while, and I've been wondering about the new keyword. Simply, should I be using it, or not? 1) With the new keyword... MyClass* myClass = new MyClass(); myClass->MyField = "Hello world!"; 2) Without the new keyword... MyClass myClass; myClass.MyField = "Hello world!"; From an implementation perspective, they don't seem that different (but I

我应该什么时候在C ++中使用新的关键字?

我一直在使用C ++,而且我一直在想新的关键字。 简单地说,我应该使用它,还是不? 1)使用新的关键字... MyClass* myClass = new MyClass(); myClass->MyField = "Hello world!"; 2)没有新的关键字... MyClass myClass; myClass.MyField = "Hello world!"; 从实现的角度来看,他们似乎没有什么不同(但我确定他们是)......但是,我的主要语言是C#,当然第一种方法是我习惯的。 难度似乎是方法1更难用于std C ++