考虑到成本,这些案件是否相同?
// case 1
int a = 5;
// case 2
int a (5);
// case 3
int a;
a = 5
考虑到成本,这些案件是否相同?
// case 1
int a = 5;
// case 2
int a (5);
// case 3
int a;
a = 5
第一和第二段与两者完全相同。 第三点是不同的,因为<>是<>转让>。 这些差异与C++标准相同。 然而,汇编者可以把这三者都视为一样。
三个星子不同,与我在一起,而我使用一个用户定义的类型而不是墨水,我稍后会回头。
T a(5); // Direct initialization
T b = 5; // Implicit conversion (5->tmp) + copy-initialization
T c; c = 5; // Default initialization + assignment
在第一种情况下,标的<代码>a <>>>>/代码”的构造是指采用<条码>的构造或从<条码>中可默示转换的类型。
struct T {
T( int ); // T a(5) will call this directly
};
在第二种情况下,由<<>简单转换<>>>/>在<条码>上创建的一组临时物体,然后该临时用途为copy Constructioncode>b。 编辑可以优化编码,仅履行简单转换<>>,取代最终目标(而不是使用该代码制造临时产品)。 但
class T {
T( T const & );
public:
explicit implicit T( int );
};
int main() {
T b = 5; // Error 1: No implicit conversion from int to T.
// Fix: remove the `explicit` from the constructor
// Error 2: Copy constructor is not accessible
}
第三个案例是建构,其次是派任。 对这种类型的要求是,可以设定违约(无论是否为构造者,还是根本不存在用户定义的构造者,汇编者将暗中加以界定)。 类型必须从<代码>int上指定,或从<代码>int到可分配给<代码>的类型<代码>。 T。 如你所看到,对树类的要求各不相同。
除了不同行动的称呼之外,还有其他重要区别,并非所有行动都能在一切情况下使用。 特别是,在一个类别中的初始清单中,你不能使用简单转换+拷贝初始版,而且你只能拥有default Construction +sign/em>的上半部分。
// OK // error // ok but different
struct test { struct test { struct test {
T x; T x; T x;
test(int v) : x(v) {} test(int v) : x=5 {} test( int v ) {
x = v;
}
In the first case the attribute x
is directly initialized with the value v
. The second case is a syntax error. The third case first default initializes and then assigns inside the body of the constructor.
回到<代码>int,所有要求均按类型得到满足,因此,汇编者对这三种情况产生的代码几乎没有任何区别,但你仍不能使用<代码>int b = 5;版本在初步编制人名单上,以初步确定分类。 此外,如果某一类别的成员属性是固定的,那么你不能使用相当于<代码>int c;c =5;(上文第三栏),因为成员属性成为<代码>const。 当进入构件时,即x = v;
above将试图修改一个固定的,汇编者将提出申诉。
对于每一单位的费用,如果可以全部使用,则其成本为<代码>int<>/code>(任何POD类型)的费用相同,但对于有违约构造的用户类别而言,则不如此,在这种情况下,T c; c = 5;
将产生default Construction>>/em>的费用,随后是>assignment<>。 在另外两个案例中,该标准明确指出,允许汇编者产生相同的准确编码(在检查制约因素时)。
头两点将没有任何区别。
《标准手册》,8.5.11。
The form of initialization (using parentheses or =) is generally insignificant, but does matter when the initializer or the entity being initialized has a class type; see below. A parenthesized initializer can be a list of expressions only when the entity being initialized has a class type.
第三个不是初始化,而是转让。
考虑到费用,
在头两起案件中,你正在制造价值5的愤怒。
在第三起案件中,你正在制造一种带有
如果你使用优化的汇编者,他们都将汇编成同一法典。 因此,他们都有同样的费用。
Yes, they all evaluate to the exact same assembler representation. You can test this e.g. with GCC by writing a dummy function and then producing the assembler output: g++ -S file.cxx -o file.s
I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...
I have been searching for sample code creating iterator for my own container, but I haven t really found a good example. I know this been asked before (Creating my own Iterators) but didn t see any ...
Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...
I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?
Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...
Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?