Memory allocation of base class and derived class constructor

For which one the space is allocated first when the derived class object is created? whether base class constructor or derived class constructor? First, allocation, the reservation of memory which you're asking about, is different from and precedes initialization (execution of a constructor that essentially sets suitable values in that memory), and the formal (our Holy Standard) and t

基类和派生类构造函数的内存分配

当派生类对象被创建时,哪个空间首先被分配? 无论是基类构造函数还是派生类构造函数? 第一, 分配,你所询问的内存保留与初始化不同(并且在初始化之前执行一个构造函数,该构造函数基本上在该内存中设置合适的值),并且 正式的(我们的圣经标准)和实际的不同之处在于对于大多数派生对象的内存是否需要连续存在,其中正式定义的“内存区域”可能不是连续的,主要是为了支持多重虚拟继承。 也就是说,在实践中,大多

How to return const Float** from a C++ function

I have a class which hold an array " float ** table ". Now I want to have member function to return it, but don't want it to be modified outside of the class. So I did this: class sometable { public: ... void updateTable(......); float **getTable() const {return table;} private: ... float **table; } This compiles OK when I call getTable with a constant obje

如何从C ++函数返回const Float **

我有一个拥有数组“ float ** table ”的类。 现在我想让成员函数返回它,但不希望它在类之外被修改。 所以我这样做了: class sometable { public: ... void updateTable(......); float **getTable() const {return table;} private: ... float **table; } 当我用一个常量对象调用getTable时,这会编译成OK。 现在我试图通过将getTable声明为“ const float **getTable() ”来使它更安全。 我得到了下

In C++ is "const" after type ID acceptable?

My co-worker is 0 for 2 on questions he has inspired (1, 2), so I thought I'd give him a chance to catch up. Our latest disagreement is over the style issue of where to put "const" on declarations. He is of the opinion that it should go either in front of the type, or after the pointer. The reasoning is that this is what is typically done by everyone else, and other styles are

在类型ID可接受后,C ++中的“const”是什么?

我的同事在他所启发的问题上是2(0,1),所以我想我会给他一个追上的机会。 我们最近的不同意见是关于在声明中放置“const”的风格问题。 他认为它应该在类型前面,或者在指针后面。 理由是这是其他人通常所做的事情,其他风格可能会令人困惑。 因此,指向常量int的指针和指向int的常量指针将分别为: const int *i; int * const i; 不过,我很困惑。 我需要一致且易于理解的规则,而我能理解“const”的唯一方法是它

Strange definitions of TRUE and FALSE macros

I have seen the following macro definitions in a coding book. #define TRUE '/'/'/' #define FALSE '-'-'-' There was no explanation there. Please explain to me how these will work as TRUE and FALSE . Let's see: '/' / '/' means the char literal / , divided by the char literal '/' itself. The result is one, which sounds reasonable for TRUE . And '-' - 

奇怪的TRUE和FALSE宏的定义

我在一本编码书中看到了以下宏定义。 #define TRUE '/'/'/' #define FALSE '-'-'-' 那里没有解释。 请向我解释这些将如何工作为TRUE和FALSE 。 让我们看看: '/' / '/'是指char字面量/除以char字面量'/'本身。 结果是一个,这对TRUE听起来是合理的。 而'-' - '-'表示char '-' ,从自身中减去。 这是零( FALSE )。 这有两个问题:首先,它不可读。 使用1和0绝对

Is the operation "false < true" well defined?

Does the C++ specification define: the existence of the 'less than' operator for boolean parameters, and if so, the result of the 4 parameter permutations? In other words, are the results from the following operations defined by the specification? false < false false < true true < false true < true On my setup (Centos 7, gcc 4.8.2) , the code below spits out what I'

操作“false <true”的定义良好吗?

C ++规范是否定义了: 对于布尔参数是否存在“少于”运算符,如果是, 4参数排列的结果? 换句话说,规范定义了以下操作的结果吗? false < false false < true true < false true < true 在我的设置(Centos 7,gcc 4.8.2)中,下面的代码吐出了我所期望的(给出C代表false的历史记录为0,true为1): false < false = false false < true = true true < false = false true < true = false 虽

Why should I not wrap every block in "try"

I have always been of the belief that if a method can throw an exception then it is reckless not to protect this call with a meaningful try block. I just posted 'You should ALWAYS wrap calls that can throw in try, catch blocks.' to this question and was told that it was 'remarkably bad advice' - I'd like to understand why. A method should only catch an exception when it c

为什么我不应该在“尝试”中包装每个块

我一直认为,如果一个方法可以抛出异常,那么不用保护有意义的尝试块来保护这个调用是鲁莽的。 我刚刚发布了'你应该总是打包可以尝试的电话,赶上块'。 对这个问题,被告知这是'非常糟糕的建议' - 我想明白为什么。 一个方法只有在能够以合理的方式处理它时才能捕获异常。 否则,传递它,希望调用堆栈中更高的方法可以理解它。 正如其他人所指出的那样,最好在调用堆栈的最高级别有一个未处理的异常处

Convert string to int C++

This question already has an answer here: How to parse a string to an int in C++? 17 answers In C++11 there are some nice new convert functions from std::string to a number type. So instead of atoi( str.c_str() ) you can use std::stoi( str ) where str is your number as std::string . There are version for all flavours of numbers: long stol(string) , float stof(string) , double stod(st

将字符串转换为int C ++

这个问题在这里已经有了答案: 如何解析一个字符串在C ++中的int? 17个答案 在C ++ 11中,有一些很好的从std::string到数字类型的新的转换函数。 所以,而不是 atoi( str.c_str() ) 您可以使用 std::stoi( str ) 其中str是你的数字作为std::string 。 有数字的所有风格的版本: long stol(string) , float stof(string) , double stod(string) ,...见http://en.cppreference.com/w/cpp/string/basic_string/sto

What's the difference between istringstream, ostringstream and stringstream? / Why not use stringstream in every case?

When would I use std::istringstream , std::ostringstream and std::stringstream and why shouldn't I just use std::stringstream in every scenario (are there any runtime performance issues?). Lastly, is there anything bad about this (instead of using a stream at all): std::string stHehe("Hello "); stHehe += "stackoverflow.com"; stHehe += "!"; Personally, I find it very rare that I want to p

istringstream,ostringstream和strings

我什么时候会使用std::istringstream , std::ostringstream和std::stringstream ,为什么我不应该在每个场景中使用std::stringstream (是否存在任何运行时性能问题?)。 最后,这有什么不好的地方(而不是使用流): std::string stHehe("Hello "); stHehe += "stackoverflow.com"; stHehe += "!"; 就我个人而言,我觉得我非常难得要进入和离开相同的字符串流。 通常我想要从一个字符串初始化一个流,然后解析它; 或

itoa function problem

I'm working on Eclipse inside Ubuntu environment on my C++ project. I use the itoa function (which works perfectly on Visual Studio) and the compiler complains that itoa is undeclared. I included <stdio.h> , <stdlib.h> , <iostream> which doesn't help. www.cplusplus.com says: This function is not defined in ANSI-C and is not part of C++, but is supported by some c

itoa功能问题

我正在使用C ++项目的Ubuntu环境中的Eclipse。 我使用itoa函数(在Visual Studio中完美工作),编译器抱怨itoa未声明。 我包括<stdio.h> , <stdlib.h> , <iostream> ,这没有帮助。 www.cplusplus.com说: 这个函数没有在ANSI-C中定义,也不是C ++的一部分,但是被一些编译器支持。 因此,我强烈建议你不要使用它。 但是,您可以使用stringstream很直接地实现这一点,如下所示: stringstream ss

Lifetime of temporaries

The following code works fine, but why is this correct code? Why is the "c_str()" pointer of the temporary returned by foo() valid? I thought, that this temporary is already destroyed when bar() is entered - but it doesn't seem to be like this. So, now I assume that the temporary returned by foo() will be destroyed after the call to bar() - is this correct? And why? std::string

临时工的生命周期

下面的代码工作正常,但为什么这是正确的代码? 为什么foo()返回的临时的“c_str()”指针有效? 我认为,当输入bar()时,这个临时文件已经被破坏 - 但它看起来并不像这样。 所以,现在我假设由foo()返回的临时对象在调用bar()后会被销毁 - 这是否正确? 为什么? std::string foo() { std::string out = something...; return out; } void bar( const char* ccp ) { // do something with the string.. } ba