今天我开始知道C++允许类型的非类型模板参数std::nullptr_t:
template<std::nullptr_t N> struct A { };
template<std::nullptr_t N> void f() { }
Run Code Online (Sandbox Code Playgroud)
对于我的生活,我无法想出任何明智的用例.任何人都可以为此提出理由吗?
使用有什么区别:
if (NULL == pointer)
Run Code Online (Sandbox Code Playgroud)
和使用:
if (pointer == NULL)
Run Code Online (Sandbox Code Playgroud)
我的教授说使用前者而不是后者,但我看不出两者之间的区别.
我是C++的新手,我并不完全确定如何以安全的方式处理数组和指针.在我的班上,我有一个叫做项目的成员:
Item * items;
Run Code Online (Sandbox Code Playgroud)
在我的类方法read()中我打开一个文件并从这个文件中读取项目.我相应地分配空间:
items = new Item[item_count];
Run Code Online (Sandbox Code Playgroud)
item_count作为文件中的变量给出,并在创建任何项目之前预先读取.在我班级的解构器中,我再次释放这样的内存:
delete[] items;
Run Code Online (Sandbox Code Playgroud)
但是如果我read()在执行解构函数之前调用该方法两次,则第一个数组的内存将无法正确释放.我想在分配新内存之前事先在read方法中释放它.但是如何检查是否已为阵列分配了一些内存items?
编辑:我知道还有很多其他的可能性,更多的"现代"方法和更舒适的解决方案.但在这种情况下,我们明确告诉使用指针和数组(仅限教育目的).
有点史前史.
我已经写了很长一段时间了.它分为几个静态库,如"utils","rsbin"(资源系统),"window",然后链接到单个可执行文件中.
它是一个跨平台引擎,适用于Windows和Android.在Windows下,我用MinGW编译它.在Android下,使用CCTools,它是本机gcc的接口.
其中一个基类是utils::RefObject代表一个类似于Windows的IUnknown的概念:它提供了一个用于确定其生命周期的引用计数器和一个从基类指针查询特定接口的方法.还有template< typename T > utils::Ref专门为这类物体设计的.它拥有std::atomic< utils::RefObject* >并在构造,赋值和销毁时自动更新其对象的引用计数,类似于std::shared_ptr.它还允许通过查询方法隐式转换不同类型的RefObject.虽然,查询对象的自身类型是低效的,因此,utils::Ref重载其大多数运算符,例如,有特定的utils::Ref< T >::Ref( T* ptr )构造函数,它只是递增传递的对象的引用计数,而general utils::Ref< T >::Ref( RefObject* ptr ),它查询其参数的例如T并抛出异常失败(不过不用担心,当然有软铸造的方法).
但是只有这两种方法引入了一个问题:你不能utils::Ref用空指针显式初始化,因为它是不明确的; 所以也有utils::Ref< T >::Ref( nullptr_t )提供一种方法.
现在,我们正在解决手头的问题.在头文件中,原型拼写完全如上,没有任何前面的std::.请注意,我也没有使用using namespace.很长一段时间,这都奏效了.
现在,我正在研究一个图形系统.它之前存在,但它相当简陋,所以我甚至没有注意到<gl.h>实际上只定义了OpenGL 1.1,而对于较新的版本,你应该通过<glext.h>.现在,有必要使用后者.但包括它打破了旧的参考类.
从错误消息来看,MinGW现在nullptr_t在原型中存在问题.我在网上做了一个快速搜索,发现它经常被称为std::nullptr_t.虽然,并非无处不在.
快速总结:直到我在标题之前包含<glext.h>之前,我nullptr_t没有编译std::或using namespace编译正常.
到目前为止,我一直在使用的网站cplusplus.com/reference表明,全球化::nullptr_t 应该是应该如何.另一方面,en.cppreference.com wiki 告诉它实际上std::nullptr_t.
一个快速测试程序,一个带有void foo( int …
这更像是一个入门级问题,但我想知道是否有一个空if语句是一个好习惯.
考虑以下代码:
void RabbitList::purge()
{
if(head == NULL)
{
//cout << "Can't purge an empty colony!" << endl;
}
else
{
//Kill half the colony
for(int amountToKill = (getColonySize()) / 2; amountToKill != 0;)
{
RabbitNode * curr = head;
RabbitNode * trail = NULL;
bool fiftyFiftyChance = randomGeneration(2);
//If the random check succeeded but we're still on the head node
if(fiftyFiftyChance == 1 && curr == head)
{
head = curr->next;
delete curr;
--size;
--amountToKill;
}
//If the random …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现以下子例程.
string& sortAndMerge(string &u, int start, int end){
if(end>=0){
int mid=(start+end)/2;
if(start<end){
sortAndMerge(u,start,mid);
sortAndMerge(u,mid+1,end);
return merge(u,start,mid,end);
}
}
return;
}
Run Code Online (Sandbox Code Playgroud)
该函数被称为sortAndMerge(s,0,s.length() - 1),这里s是一个字符串.由于第3个参数最终可能是负数,我需要子程序来忽略这些情况.
我收到以下错误: -
return-statement with no value, in function returning ‘std::string& {aka std::basic_string<char>&}’ [-fpermissive].
Run Code Online (Sandbox Code Playgroud)
我有两个问题.
1)有没有办法可以从具有除void之外的返回类型的函数返回任何内容.
2)什么时候可以使用return关键字什么也不用NULL.
Null没有宣布?
我的代码:
// Include necessary libraries
#include <cstdlib> // Exits
#include <iostream> // I/O
#include <cstring> // String functions
using namespace std;
int main(int argc, char *argv[])
{
//Declare local Constants and Variables
const char SOKINP[19] = "23456789TtJjQqKkAa"; // Valid Input Characters
char acCards [5]; // Array to hold up to five cards (user input)
bool bErr; // Loop on Error (Calculated)
int i, // Loop variable (Calculated)
iNbrCrd, // Number of Cards 2-5 (user input)
iNAces, // Number of …Run Code Online (Sandbox Code Playgroud) 我知道NULL是#defined为0.它似乎是一个int转换为指针类型的常量.那么当有两个重载函数时会发生什么:一个采用指针类型,另一个采用int类型.这是如何工作的nullptr?
#include <iostream>
using namespace std;
void callMe(int* p)
{
cout << "Function 1 called" << endl;
}
void callMe(int i)
{
cout << "Function 2 called" << endl;
}
int main()
{
callMe(nullptr);
callMe(NULL);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道callMe(nullptr)肯定会调用第一个函数.但是哪个函数会被调用callMe(NULL)?我在我的电脑上编译了这段代码.只有一个警告.
g++ -std=c++11 nullptr_type.cpp
nullptr_type.cpp: In function 'int main()':
nullptr_type.cpp:44:14: warning: passing NULL to non-pointer argument 1 of 'void callMe(int)' [-Wconversion-null]
callMe(NULL);
^
Run Code Online (Sandbox Code Playgroud)
我的代码编译没有任何问题,当我运行它时,我看到callMe(NULL)称为函数2. Visual Studio也编译代码,它调用函数2.
但是,当我尝试在在线GeeksForGeeks IDE上编译我的代码时,我遇到了一些编译器错误.
https://ide.geeksforgeeks.org/UsLgXRgpAe
我想知道为什么会出现这些错误. …
我正在printf(const char * format, ...)为一个类实现一个类似的方法,在调用执行实际写入之前,我需要确定它的输出的确切大小,仅给出提供的format和给出的参数。va_listvsprintf()
是否有一个函数可以使用format和va_list来生成输出的确切长度?
c++ ×9
c++11 ×2
arrays ×1
coding-style ×1
function ×1
if-statement ×1
null ×1
nullptr ×1
overloading ×1
printf ×1
std ×1
templates ×1