可能重复:
复制初始化和赋值初始化之间的C++是否存在差异?
我是C++的新手,我很少看到有人使用这种语法来声明和初始化变量:
int x(1);
Run Code Online (Sandbox Code Playgroud)
我试过,编译器没有抱怨,输出和int x = 1相同,它们实际上是一样的吗?
非常感谢大家.
我想创建一个具有以下类型的C++类:
使用此:想想GC的"Root"对象.
这在C++中是否可行?特别是,我正在使用g ++.愿意切换到铿锵声.模板或宏解决方案都很好.
谢谢!
class A{
private:
string a;
public:
A():a("hello world"){};
A(const string & arg):a(arg){};
void put()const{cout<<a;};
};
A aaa();
A bbb;
Run Code Online (Sandbox Code Playgroud)
那么,有什么之间的区别A aaa();和A bbb;是aaa一个功能?
我正在阅读Gilles Dowek的编程原理:
他说,也可以在不给它初始值的情况下声明变量,并且我们必须小心不要使用已经声明没有初始值并且没有赋值的变量.这会产生错误.
注意:本书的作者提到在Java上声明没有初始值的变量的可能性.
那么,为什么这个变量声明有效呢?我什么时候开始使用它?
我真的不知道如何在Swift中声明我的变量,我有四个选项:
var value = 0.0 // I know this one declares value as a Double with the number 0.0
var value: Float = 0.0 // This one declares value as a Float with the number 0.0
var value: Float? // I really don't get the difference between this
var value: Float! // and this, I just know that both of them are used to declare a variable without a value, am I right ?
Run Code Online (Sandbox Code Playgroud)
我只是想声明一个不归因于值的浮点数,最好的方法是什么?
我前段时间正在阅读C++ 11标准草案并遇到过这个草案(见§8.3.6,第204页):
void g(int = 0, ...); // OK, ellipsis is not a parameter so it can follow
// a parameter with a default argument
void f(int, int);
void f(int, int = 7);
void h() {
f(3); // OK, calls f(3, 7)
void f(int = 1, int); // error: does not use default
// from surrounding scope
}
void m() {
void f(int, int); // has no defaults
f(4); // error: wrong number of arguments
void …Run Code Online (Sandbox Code Playgroud) 当第一个声明被注释掉时,以下命名空间定义无法编译.如果第一个声明foo是取消注释的,那么它编译就好了.
namespace Y
{
//void foo();
void ::Y::foo(){}
}
Run Code Online (Sandbox Code Playgroud)
标准中的相关部分(§8.31)说:
当declarator-id被限定时,声明应引用先前声明的成员
据我所知,此规则可防止将名称引入其他名称空间.我想知道是否可以放宽该规则以允许引用当前命名空间的qualified-id.
在C++中,多次声明变量会在编译期间显示错误.例如:
int x;
int x;
Run Code Online (Sandbox Code Playgroud)
在多次声明函数时,编译期间不会显示任何错误.例如:
int add(int, int);
int add(int, int);
Run Code Online (Sandbox Code Playgroud)
为什么在C++中有这种区别?
我对[dcl.array]/1感到困惑:
在TD的声明中,D表格
D1 [ constant-expression opt ] attribute-specifier-seq opt
并且声明T D1中的标识符的类型是" derived-declarator-type-list T",则D的标识符的类型是数组类型; ...
考虑声明:
int (*p)[42];
Run Code Online (Sandbox Code Playgroud)
该声明满足上述语法(并且不满足前面段落中描述的语法),因此本段应该适用,因此我们得出结论,类型p是数组类型.但是,我们知道该类型的p就是pointer to array of 42 int,这是一个指针类型.
我错过了什么吗?或者pointer to array of 42 int确实是阵列类型?
declaration ×10
c++ ×8
variables ×4
definition ×2
arrays ×1
function ×1
java ×1
namespaces ×1
object ×1
qualifiers ×1
scope ×1
swift ×1
syntax ×1