我可以在C++中的条件语句中声明两个变量.编译器给出了错误,但我认为我应该得到一个意见:
int main()
{
double d1 = 0;
if((double d2 = d1) || (double d3 = 10))
cout << "wow" << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有两个类:A和B.类A的一些方法需要使用类B而相反(类B有需要使用类A的方法).
所以我有:
class A;
class B {
method1(A a) {
}
}
class A {
method1(B b) {
}
void foo() {
}
}
Run Code Online (Sandbox Code Playgroud)
一切正常.
但是当我尝试从B :: method1调用类A的foo()时:
class B {
method1(A a) {
a.foo();
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的结果是前向声明的编译错误和 不完整类型的使用.但为什么会这样呢?(我在使用之前已经宣布了A级?)
我想为主perl程序和其他包设置所有常见声明的专用包,而不是在每个头中重复这些声明.我肯定错了,但无法弄清楚背后的理由:
我们假设:
- 我已经在包my_common_declarations.pm中设置了我的公共数据.
- 我想在另一个包中使用这些数据, 例如my_perl_utils.pm.
#!/usr/bin/perl -w
package my_perl_utils;
use parent qw(Exporter);
our @EXPORT_OK = qw(f1 f2);
use my_common_declarations qw(debugme);
my %setup = &debugme;
my $DEBUGME = $setup{setup}{debugme};
# This generates this error : "Use of uninitialized value"
use constant true => $setup{setup}{'true'};
print "=" x25, "\nDEBUG true :\nimport = " . $setup{setup}{'true'} . "\nconstant = " , true , "\n", "=" x25, "\n";
sub f1{
# some rationals using the true or false …Run Code Online (Sandbox Code Playgroud) 使用int someInts[3]和使用声明一个新的整数数组有什么区别int* someInts = new int[3]?
我无法编译此代码,因为函数声明依赖于类声明,而类声明依赖于函数声明.请帮忙.
#include <iostream>
using namespace std;
void simulate(Policy& p);
class Policy {
public:
Policy(int);
int x;
void eval();
};
int main() {
Policy p(23);
return 0;
}
Policy::Policy(int y) { x = y; }
void Policy::eval() { simulate(this); }
void simulate(Policy& p) { cout << ++p.x << endl; }
Run Code Online (Sandbox Code Playgroud) 我正在检查我的应用程序"坏代码"使用ReSharper.
我有以下内容if-statement:
if (Utility.Compare(Utility.ExtractRangeFromArray(bufferRx, 0, bytesRead), new byte[] { U_EOT}))
{
// EOT (End of transmission) received, break from while
break;
}
Run Code Online (Sandbox Code Playgroud)
ReSharper告诉我改变:new byte[]到new[]
我的问题:什么是最好的选择?为什么我不应该声明变量?
当我编译这段代码时
#include <stdio.h>
int *foo();
int main()
{
*foo()++;
return 0;
}
int *foo()
{
static int bar;
return &bar;
}
Run Code Online (Sandbox Code Playgroud)
Clang给我看错:
static2.c:7:8: error: expression is not assignable
Run Code Online (Sandbox Code Playgroud)
为什么这是非法的?我想bar有静态存储持续时间,所以它的生命周期是整个程序的执行.虽然bar本身不可见main(),但指针应该能够修改它.
这个版本foo()也不起作用,Clang给了我同样的错误:
int *foo()
{
static int bar;
static int* ptr = &bar;
return ptr;
}
Run Code Online (Sandbox Code Playgroud) 我搜索了互联网和StackOverflow关于const_cast <>及其引起的混乱,我找到了有用的东西,但是我还有一个问题.
考虑到这段代码,
#include <iostream>
using namespace std;
int main(void)
{
const int a = 1;
int *p = const_cast<int*>(&a);
*p = 2;
cout << "value a="<< a << endl;
cout << "value *p=" <<*p << endl;
cout << "address a=" <<&a << endl;
cout << "address p=" <<p << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
值a = 1
值*p = 2
地址a = 0x69fef8
地址p = 0x69fef8
我发现这样的代码可能导致未定义的行为.(例如,编译器可能会将所有a's 替换为's以1进行优化,因此强制转换没有意义)
我也发现了这句话:
如果抛弃已显式声明为const的对象的常量,并尝试修改它,则结果是未定义的.
但是,如果丢弃未明确声明为const的对象的常量,则可以安全地修改它.
还有这个:
请注意,C++提供
const_cast了删除或添加常量的变量.但是,在删除constness时,它应该用于从引用/指针中删除constness,以指向原本不是常量的东西. …
我在Boost库中读取了类address_v4的源代码,并且有几个用BOOST_ASIO_DECL声明的构造函数(定义为内联)
/// Construct an address from raw bytes.
BOOST_ASIO_DECL explicit address_v4(const bytes_type& bytes);
/// Construct an address from a unsigned long in host byte order.
BOOST_ASIO_DECL explicit address_v4(unsigned long addr);
Run Code Online (Sandbox Code Playgroud)
(从此处http://www.boost.org/doc/libs/1_64_0/boost/asio/ip/address_v4.hpp)
# define BOOST_ASIO_DECL inline
Run Code Online (Sandbox Code Playgroud)
(从这里http://www.boost.org/doc/libs/1_64_0/boost/asio/detail/config.hpp)
那么,为c ++构造函数指定“内联”的目的是什么?它与函数的含义相同还是具有不同的含义?
在c中,声明给定类型的变量的语法如下:
// <variable-type> <variable-name>
// for example:
int foo;
Run Code Online (Sandbox Code Playgroud)
要声明一个指针,可以在类型后面或变量名前面使用asterisk(*)。
int* bar;
int *foobar;
Run Code Online (Sandbox Code Playgroud)
该指针也可以不指向单个元素,而是指向给定数据类型的元素数组。
例如,一个字符数组(一个字符串):
char* oneString = "this is a string";
Run Code Online (Sandbox Code Playgroud)
这样的字符串的替代声明(编辑:尽管绝对不是等效的,如注释中所指出),然后如下所示:
//<variable-type> <variable-name>[<count>]
//example:
char anotherString[1024] = "this is another string";
Run Code Online (Sandbox Code Playgroud)
在用于比较的c#中,您可以采用另一种方法:
int[] intArray;
Run Code Online (Sandbox Code Playgroud)
什么是在有效的声明数组的考虑变量名,而不是它的类型?
(不是另一个问题,仅是为了提供比较设计决策的手段)
人们创建c#,违反此规则并将数组声明移至类型本身的原因可能是什么?
declaration ×10
c++ ×6
arrays ×2
c ×2
c# ×1
conditional ×1
const ×1
const-cast ×1
constants ×1
constructor ×1
definition ×1
inline ×1
package ×1
perl ×1
pointers ×1
resharper ×1
shared ×1
static ×1
syntax ×1
types ×1