下面的代码是从这里:
#include <streambuf> // for std::streambuf
#include <ostream> // for std::ostream
class fdoutbuf
: public std::streambuf
{
public:
explicit fdoutbuf( int fd );
//...
};
class fdostream
: public std::ostream
{
protected:
fdoutbuf buf;
public:
explicit fdostream( int fd )
: buf( fd ), std::ostream( &buf ) // This is not allowed.
// buf can't be initialized before std::ostream.
{}
//...
};
Run Code Online (Sandbox Code Playgroud)
我真的不明白这个评论.为什么"在std :: ostream之前无法初始化buf"?我可以用一些帮助来理解这个吗?
在 python 中,我可以在字典中有一个字典,如下所示:
dict = { 'a': { 1:1, 2:2, 3:3 },
'b': { 1:1, 2:2, 3:3 },
'c': { 1:1, 2:2, 3:3 } }
Run Code Online (Sandbox Code Playgroud)
在 C++ 中,我必须在另一个地图中使用地图:
std::map< std::string, std::map<int, int> > map1
Run Code Online (Sandbox Code Playgroud)
但是如何实现与 python 示例相同的结构?一直找不到这方面的任何例子。
std::map< std::string, std::map<int, int, int> > ??
std::map<std::string, std::map<int, int>, std::map<int, int>, std::map<int, int> > ??
Run Code Online (Sandbox Code Playgroud) 我有这段代码
class Osoba{
Osoba(char* imie,int wiek){
this->imie=new char[strlen(imie)+1];
strcpy(this->imie,imie);
this->wiek=wiek;
cout<<"Utworzono Osobe "<<this->imie<<endl;
}
Osoba(Osoba& x){
Osoba(x.imie,x.wiek);
}
[...]
Run Code Online (Sandbox Code Playgroud)
当我调用复制构造函数时,它不起作用(创建和销毁对象).
编辑:如果我使用
Osoba(Osoba& x): Osoba(x.imie,x.wiek){
Run Code Online (Sandbox Code Playgroud)
我明白了 type 'class Osoba' is not a direct base of 'Osoba'
怎么做的?
我正在使用 Doxygen 为我当前的项目生成一个 API,并且发生了一些奇怪的行为。基本上,如果我使用初始化列表在类的构造函数中设置成员数组,Doxygen 不会产生正确的输出。
这是一个简单的测试类:
#ifndef TEST_HPP
#define TEST_HPP
class TestClass {
public:
/** Constructor Version 1 */
TestClass() : v{0,0,0} { }
/** Constructor Version 2 */
// TestClass() {
// v[0] = 0;
// v[1] = 0;
// v[2] = 0;
// }
protected:
/** my little array */
float[3] v;
};
#endif // TEST_HPP
Run Code Online (Sandbox Code Playgroud)
如果我使用构造函数的版本 1 在文件上运行 doxygen,我会得到一个相对空的类的 HTML 文件,没有构造函数文档,也没有提到我的变量 v。如果我注释掉版本 1 并使用版本 2,Doxygen 会正确生成类的文档。
我知道这种类型的数组设置对 C++11 来说是新的,但它是初始化还是它在初始化列表中完成的事实?如果有人知道导致这种行为的原因,我将不胜感激,因为我们在整个代码中使用这些类型的初始化程序,并且我希望在必要时避免进行彻底的更改。
以下代码无法与g ++ 4.8.2链接:
#include <map>
struct Foo
{
constexpr static int foo = 1;
};
static std::map<int, int> map {{1, Foo::foo}};
int main()
{
return Foo::foo;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
g++ -std=c++11 -o foo foo.cc
/tmp/ccZXCwiK.o: In function `__static_initialization_and_destruction_0(int, int)':
foo.cc:(.text+0x51): undefined reference to `Foo::foo'
Run Code Online (Sandbox Code Playgroud)
如果我评论出地图,事情就好了.这是一个编译器错误,还是我在标准中缺少的一些极端情况?
我正在学校里完成一项任务,我们应该在我们自己的List类中创建一个构造函数,它将初始化列表作为参数.
这是我希望能够做到的:
List ourList {1, 2, 3};
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止:
List::List(std::initializer_list<int> il)
{
head_ = copy(il.begin(), il.end());
}
List_Node* List::copy(std::initializer_list<int>::iterator begin,
std::initializer_list<int>::iterator end)
{
if(begin == end)
return nullptr;
List_Node* new_list = new List_Node(*begin);
List_Node* node = copy(begin++, end);
new_list->next_ = node;
return new_list;
}
Run Code Online (Sandbox Code Playgroud)
以我的拙见,这应该很有效.但是,当我尝试初始化(List list {1,2,3};
)时,我得到一个seg-fault.有人可以解释一下我在这里做错了吗?
我正在制作一个游戏,其中我控制游戏对象的主要类是"inGame".将在"inGame"中组成其他几个自定义类.
喜欢,
class mouse
{
int x, y;
bool alive;
float speed;
public:
mouse(int xx, int yy, float spd, bool alv)
{
x = xx; y = yy; speed = spd; alive = alv;
}
// member functions
};
class inGame
{
mouse m1;
dog d1; // single object which are easy to construct
public:
inGame() : d1(200, 400, 1.5, false), m1(40, 40, 0.5, false) //This works fine
{} //coordinates fixed and set by me
// member functions
};
Run Code Online (Sandbox Code Playgroud)
但现在可以说我想要3个鼠标.所以我想到了m1 …
我可以看到std :: string只有一个CTOR initializer_list
:string (initializer_list<char> il);
所以初始化列表应该与chars一起使用,对吧?为什么std::string{"some_str"}
有效,它得到了const char*
,对吧?
c++ stdstring initialization-list uniform-initialization c++11
有一些C++代码.我想知道为什么在初始化列表(:OTMixerMgr(OTMediaType_Audio, oBridgeInfo)
)中可以将两个值用于单个类成员?因为通常只有一个值用于单个类成员,例如
ClassName::ClassName(): fisrtMember(firstValue) {...}.
OTMixerMgrAudio::OTMixerMgrAudio(OTObjectWrapper<OTBridgeInfo*> oBridgeInfo)
:OTMixerMgr(OTMediaType_Audio, oBridgeInfo)
{
m_phPullThread[0] = NULL;
m_phPullCond = NULL;
m_nLastTimerPull = 0;
m_bStarted = false;
m_bPaused = false;
OT_ASSERT(m_phProducersMutex = tsk_mutex_create());
OT_ASSERT(m_phConsumersMutex = tsk_mutex_create());
m_oMixerAudio = OTMixerAudio::New(oBridgeInfo);
m_bValid = (m_phConsumersMutex && m_oMixerAudio);
}
Run Code Online (Sandbox Code Playgroud)
此代码取自远程呈现项目,https://code.google.com/p/telepresence/source/browse/trunk/source/OTMixerMgrAudio.cc? r = 118,第31行
谢谢!