我刚刚看了一个由GMAN评论说
class A
{
public:
A() :
m_ptr() // m_ptr is implicitly initialized to NULL
{ }
};
Run Code Online (Sandbox Code Playgroud)
应该优先考虑
class A
{
public:
A() :
m_ptr(NULL) // m_ptr is explicitly initialized to NULL
{ }
};
Run Code Online (Sandbox Code Playgroud)
请注意NULL第一个示例中缺少的内容.
GMan对吗?这可能有点主观,所以"你更喜欢空的初始化器进行默认初始化吗?" 可能更合适.
此外,如果您更喜欢空的初始化器,这是否适用于其他整体成员?
class B
{
public:
B() :
m_count(),
m_elapsed_secs()
{}
private:
std::size_t m_count;
float m_elapsed_secs; //the elapsed time since instantiation
};
Run Code Online (Sandbox Code Playgroud)
当然,请捍卫您的观点,并说明为什么一个人应该优先于另一个.
有没有办法std::array在类的构造函数初始化程序中填充?
现在我正在填充它很长的路:
class Matrix3x3 {
//...
private:
std::array<double, 9> _indicies;
};
//...
Matrix3x3::Matrix3x3(double m00, double m01, double m02, double m10, double m11, double m12, double m20, double m21, double m22) : _indicies() {
_indicies[0] = m00; _indicies[1] = m01; _indicies[2] = m02;
_indicies[3] = m10; _indicies[4] = m11; _indicies[5] = m12;
_indicies[6] = m20; _indicies[7] = m21; _indicies[8] = m22;
}
Run Code Online (Sandbox Code Playgroud)
-编辑 -
Visual Studio 2010没有完整的c ++ 11支持(它仍然将std :: array列为std::tr1::array命名空间的typedef !).
尝试包括:
1. Matrix3x3::Matrix3x3(const std::initializer_list<std::array<double, 9> …Run Code Online (Sandbox Code Playgroud) 请考虑以下示例.当bar被构造,它给它的基本类型(foo)构造的地址my_member.y,其中my_member是还没有被初始化的数据成员.
struct foo {
foo(int * p_x) : x(p_x) {}
int * x;
};
struct member {
member(int p_y) : y(p_y) {}
int y;
};
struct bar : foo
{
bar() : foo(&my_member.y), my_member(42) {}
member my_member;
};
#include <iostream>
int main()
{
bar my_bar;
std::cout << *my_bar.x;
}
Run Code Online (Sandbox Code Playgroud)
这个定义得很好吗?获取未初始化对象的数据成员的地址是否合法?我发现了一个关于传递对未初始化对象的引用的问题,但它并不完全相同.在这种情况下,我在未初始化的对象上使用成员访问运算符 ..
这是真的,一个对象的数据成员的地址不应该被初始化改变,但这并不一定能使服用很好地定义了地址.此外,成员访问运营商的ccpreference.com页面有这样的说法:
即使没有必要,也会评估两个运算符的第一个操作数(例如,当第二个操作数命名为静态成员时).
我理解这意味着在&my_member.y my_member将被评估的情况下,我认为这很好(int x; x;似乎很好),但我找不到文件来支持它.
c++ object-lifetime language-lawyer ctor-initializer member-access
这是我经常遇到的问题.以下示例说明了它:
struct A {
int m_SomeNumber;
};
struct B {
B( A & RequiredObject );
private:
A & m_RequiredObject;
};
struct C {
C( );
private:
A m_ObjectA;
B m_ObjectB;
};
Run Code Online (Sandbox Code Playgroud)
C看起来像这样的构造函数的实现:
C::C( )
: B( m_ObjectA )
{ }
Run Code Online (Sandbox Code Playgroud)
由于未定义初始化顺序,因此在调用m_ObjectA构造函数时可能未初始化m_ObjectB,从而导致未定义的行为.强制某个初始化顺序的一种方法是使成员指针并在构造函数体中初始化它们,从而强制正确的顺序,但由于几个原因这很难看.有没有办法使用构造函数的初始化列表强制某个初始化顺序?如果没有,你有任何其他建议如何处理这个.
从构造函数初始值设定项中抛出异常的最佳方法是什么?
例如:
class C {
T0 t0; // can be either valid or invalid, but does not throw directly
T1 t1; // heavy object, do not construct if t0 is invalid, by throwing before
C(int n)
: t0(n), // throw exception if t0(n) is not valid
t1() {}
};
Run Code Online (Sandbox Code Playgroud)
我想也许制作包装纸,比如说t0(throw_if_invalid(n)).
处理此类案件的做法是什么?
C++ 中的 const 字段必须在初始化列表中初始化,这使得从构造函数参数计算相互依赖的值变得非常重要。
例如,将这段 java 代码翻译成 c++ 的最佳方法是什么?
public class SomeObject {
private final String some_string;
private final int some_int;
public SomeObject(final String input_filename){
SomeReader reader(input_filename);
some_string = reader.getString();
some_int = reader.getInt();
reader.close();
}
}
Run Code Online (Sandbox Code Playgroud)
我想过在SomeObject中封装一个子对象,但这只是转移问题;或使用静态方法构造对象:
class SomeObject {
private:
const std::string some_string;
const int some_int;
public:
static SomeObject unserialize(const char * input_filename){
SomeReader reader = new SomeReader(input_filename);
string str = reader.get_string();
int i = reader.get_int();
reader.close();
SomeObject obj(str, i);
return obj;
};
SomeObject(const std::string str, const int i) …Run Code Online (Sandbox Code Playgroud) 这两个构造函数声明之间有什么区别:
class Fruit {
private:
int price;
public:
Fruit(int x): price(x)
{
}
};
Run Code Online (Sandbox Code Playgroud)
VS
class Fruit {
private:
int price;
public:
Fruit(int x)
{
price = x;
}
};
Run Code Online (Sandbox Code Playgroud)
在继承的情况下我见过的第一个.
据我所知,这不是一个重复的问题.如果你发现一个人可以随意关闭这个问题.
我对C/C++非常陌生,不确定调用该方法的方法.但那就是为什么我在这里试图找到答案.让我给你举个例子
MyClass::MyClass() : valueOne(1), valueTwo(2)
{
//code
}
Run Code Online (Sandbox Code Playgroud)
其中valueOne和valueTwo是在主体外部分配值的类属性,这是什么方法,为什么这样做.为什么不这样做呢
MyClass::MyClass()
{
valueOne = 1;
valueTwo = 2
//code
}
Run Code Online (Sandbox Code Playgroud)
如果有人能帮助我,那将是伟大的.
我不知道这个“功能”叫什么,所以我也无法用谷歌搜索它,如果标题没有意义,我很抱歉。我最近查看了suckless dwm的源代码并看到了这段代码:(来自dwm.c)
static int (*xerrorxlib)(Display *, XErrorEvent *);
Run Code Online (Sandbox Code Playgroud)
还有这个:
static void (*handler[LASTEvent]) (XEvent *) = {
[ButtonPress] = buttonpress,
[ClientMessage] = clientmessage,
[ConfigureRequest] = configurerequest,
[ConfigureNotify] = configurenotify,
[DestroyNotify] = destroynotify,
[EnterNotify] = enternotify,
[Expose] = expose,
[FocusIn] = focusin,
[KeyPress] = keypress,
[KeyRelease] = keypress,
[MappingNotify] = mappingnotify,
[MapRequest] = maprequest,
[MotionNotify] = motionnotify,
[PropertyNotify] = propertynotify,
[UnmapNotify] = unmapnotify
};
Run Code Online (Sandbox Code Playgroud)
这是什么void (*handler[LASTEvent]) (XEvent *)意思 ?它叫什么以及为什么用它?
现代 C++ 中是否有一种方法可以在从访问指针参数派生的类中初始化 const 值?并且该指针可能为空,所以基本上在检查指针之后?
例如
// Dummy class to pass as argument in Class A through a pointer, just for this given example
class Zero
{
public:
int i = 0;
};
class A
{
public:
A(Zero* z) : isZero(z->i == 0) // in this simple e.g.
// could be: isZero(z == nullptr ? false : z->i), but immagine having a more complex structure instead,
// this kind of verbose way will work,
// but i am asking if …Run Code Online (Sandbox Code Playgroud) ctor-initializer ×10
c++ ×9
constructor ×4
constants ×2
c ×1
c++-faq ×1
c++11 ×1
declaration ×1
exception ×1
oop ×1
pointers ×1
stdarray ×1