草庐IT

non-class

全部标签

c++ - C/C++ : passing a struct/class with a member array to a function by value

structA{intV[100];};voidf(Aa){a.V[0]=30;}intmain(){Aa;a.V[0]=10;f(a);cout我期望30作为输出,但我得到了10。我知道,如果参数是按值传递的,数组(也是类/结构的成员)也是按引用传递的。相反,当成员时,它们似乎是通过拷贝传递的。是真的吗? 最佳答案 按值将数组作为参数传递给函数会导致它衰减为指向第一个元素的指针,这就像按引用传递一样。将一个包含数组(不是指针)的对象按值传递给函数会导致该对象(包括数组)被复制到函数的参数中。如果您想在调用站点看到该修改,请通过非常

c++ - 初学者 c++ : virtual functions in a base class

我正在编写一些代码,其中我定义了以下基类。classChorus{public://Destructorvirtual~Chorus();//callbackfunctionvirtualintcallback(void*outputBuffer,void*notUsed,unsignedintnBufferFrames,doublestreamTime,RtAudioStreamStatusstatus,void*userData);virtualvoidinitializeDelayBuffer(void);virtualvoiddestroyDelayBuffer(void);}

C++ : How to ensure that a class member variable is modifiable only within a certain method

我在MacOSSierra上使用带有clang的C++14。我想通过设计来执行规则。以下是规则。我的类中有一个成员变量说:unsignedintm_important_num;我的类中有4个方法。fun1();fun2();fun3();fun4();目标:我只希望fun2()能够更改m_important_num的值。问题:如果fun2()以外的任何方法更改变量,是否有可能使其成为编译器错误?一种可能的方法是将其声明为const以某种方式授权fun2()更改const变量?这是一个好的解决方案吗?或者他们有更好的解决方案吗?次要问题:尝试做这样的事情是错误的设计吗?

c++ - 如何摆脱 - 来自 NULL 的 "warning: converting to non-pointer type ' 字符”?

我有这段代码:intmyFunc(std::string&value){charbuffer[fileSize];....buffer[bytesRead]=NULL;value=buffer;return0;}行-buffer[bytes]=NULL给我一个警告:convertingtonon-pointertype'char'fromNULL。我如何摆脱这个警告? 最佳答案 不要使用NULL?它一般是为指针保留的,你没有指针,只有一个简单的char。只需使用\0(空终止符)或简单的0。

c++ - 同一个类(class),不同的尺寸......?

看代码,你就明白我的困惑了。测试.hclassTest{public:#ifndefHIDE_VARIABLEintm_Test[10];#endif};啊啊啊classTest;intGetSizeA();Test*GetNewTestA();Aho.cpp//#defineHIDE_VARIABLE#include"Test.h"#include"Aho.h"intGetSizeA(){returnsizeof(Test);}Test*GetNewTestA(){returnnewTest();}Bho.hclassTest;intGetSizeB();Test*GetNewTes

c++ - <function> 不是 <class> 的成员

我已将我的函数“Credit”声明为带有一些参数的私有(private)成员。我的观察是,每当我尝试不带任何参数进行编译时,编译器都会成功编译。但是一旦我用函数中的参数进行编译,编译器就会报错'Transaction::Credit'isnotamemberof'Transaction'这是我的代码classTransaction:publicMenu{private:voidCredit(intdepost);//{return0;}public:voidDeposit();voidWithdraw(){}voidTransfer(){}};voidTransaction::Depo

c++ - 为什么将 "pointer to pointer to non-const"转换为 "pointer to pointer to const"是不合法的

将非const指针转换为const指针是合法的。那为什么将指向非const的指针转换为指向const的指针是不合法的呢?例如,为什么下面的代码是非法的:char*s1=0;constchar*s2=s1;//OK...char*a[MAX];//akachar**constchar**ps=a;//error! 最佳答案 来自标准:constcharc='c';char*pc;constchar**pcc=&pc;//notallowed*pcc=&c;*pc='C';//wouldallowtomodifyaconstobject

c++ - boost multi_index : retrieve unique values of a non-unique key

我有一个boost::multi_index_container其元素是这样的结构:structElem{Aa;Bb;Cc;};主键(在数据库意义上)是a和b的composite_key。其他键的存在是为了执行各种类型的查询。我现在需要检索一组c的所有不同值。这些值是无论如何不是唯一的,而是遍历所有条目(尽管是有序的),或者使用std::unique似乎很浪费,考虑到c的不同值的数量预计将我是否缺少更有效地获得此结果的简单方法? 最佳答案 我搜索了Boost.MultiIndex文档,但似乎无法找到一种方法来执行您想要的操作。我很想

添加ng class的课程单击孩子的父母

我有以下问题。我需要设置active与父母的课div从ChildDiv点击,该父母内部。为了说明我将提供一个代码。并省略它的一部分以确保可读性。html...SelectCSS.selected{border:2pxsolid#ffbe10;}.selected-cta{background-color:#ffbe10;}如你所见,我有offer__container获取一些数据并进行NG重复,我需要能够点击offer__container__cta添加active样式对父容器的样式,并跟踪,就像我单击另一个div通过NG重复渲染,应采用主动样式并将其转移到该div。最好还是想设置某些样式of

c++ - 为什么 boost::interprocess::managed_shared_ptr to non-const 不能转换为 managed_shared_ptr to const

据我了解,以下内容对boost::shared_ptr有效:boost::shared_ptrptr;...boost::shared_ptrc_ptr=ptr;//Valid相同的行为不适用于boost::interprocess::managed_shared_ptr。为什么? 最佳答案 boost::interprocess::managed_shared_ptr实际上不是共享指针;它只是一个辅助类,您可以使用它来定义一个类的类型。来自interprocessdocs:typedefmanaged_shared_ptr::ty