在C++中通常用某种前缀命名成员变量来表示它们是成员变量而不是局部变量或参数.如果你来自MFC背景,你可能会使用m_foo.我myFoo偶尔也见过.
C#(或者可能只是.NET)似乎建议只使用下划线,如_foo.这是否允许C++标准?
是否可以在头文件中转发声明STL容器?例如,请使用以下代码:
#include <vector>
class Foo
{
private:
std::vector<int> container_;
...
};
Run Code Online (Sandbox Code Playgroud)
我希望能够做到这样的事情:
namespace std
{
template <typename T> class vector;
}
class Foo
{
private:
std::vector<int> container_;
...
};
Run Code Online (Sandbox Code Playgroud)
可以这样做吗?
我有一个头文件,有一些前向声明但是当我在实现文件中包含头文件时,它包含在先前的前向声明的包含之后,这会导致像这样的错误.
error: using typedef-name ‘std::ifstream’ after ‘class’
/usr/include/c++/4.2.1/iosfwd:145: error: ‘std::ifstream’ has a previous declaration.
class ifstream;
class A
{
ifstream *inStream;
}
// End of A.h
#include <ifstream>
using std::ifstream;
#include "A.h"
// etc
Run Code Online (Sandbox Code Playgroud)
什么是解决这个问题的常态?
提前致谢.
我想知道在C++中是否可以这样做:
template <typename T> class A { T link;};
template <typename U> class B { U link;};
class AA : A<BB> {};
class BB : B<AA> {};
Run Code Online (Sandbox Code Playgroud)
因为它会产生错误:
error: ‘BB’ was not declared in this scope
error: template argument 1 is invalid
Run Code Online (Sandbox Code Playgroud)
我试图使用预期声明:
class AA;
class BB;
class AA : A<BB> {};
class BB : B<AA> {};
Run Code Online (Sandbox Code Playgroud)
但它不起作用:
In instantiation of ‘A<AA>’:
error: ‘A<T>::s’ has incomplete type
error: forward declaration of ‘struct AA’
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助,