考虑以下程序(请参阅此处的实时演示)
import std.stdio;
class myclass
{
public:
int get_a()
{
return a;
}
private:
int a=3;
}
int main()
{
myclass m; // It should be myclass m=new myclass();
writefln("%d",m.get_a());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
C++支持自动(堆栈分配)和动态(堆分配)对象.但为什么每个类对象必须在D中动态分配?为什么D不支持堆栈分配的对象?
另一个令人惊讶的事情是,ideone编译器错误为:
prog.d(14): Error: null dereference in function _Dmain
Run Code Online (Sandbox Code Playgroud)
但是当我在dmd2编译器上的本地机器上尝试它时,它给了我运行时错误而不是编译时错误.为什么?为什么这个程序的行为有所不同?以下是我在dmd2给出的本地机器上的错误.
object.Error@(0): Access Violation
----------------
0x00402056
0x00405F9B
0x00405EB1
0x00403D93
0x7651EE6C in BaseThreadInitThunk
0x7758377B in RtlInitializeExceptionChain
0x7758374E in RtlInitializeExceptionChain
Run Code Online (Sandbox Code Playgroud) 我从这里学习C语言中与时间相关的函数.他们使用以下示例演示了strftime()函数:
#include <stdio.h>
#include <time.h>
#define LEN 150
int main ()
{
char buf[LEN];
time_t curtime;
struct tm *loc_time;
//Getting current time of system
curtime = time (NULL);
// Converting current time to local time
loc_time = localtime (&curtime);
// Displaying date and time in standard format
printf("%s", asctime (loc_time));
strftime (buf, LEN, "Today is %A, %b %d.\n", loc_time);
fputs (buf, stdout);
strftime (buf, LEN, "Time is %I:%M %p.\n", loc_time);
fputs (buf, stdout);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我对printf()中的%m说明符进行了调整.它说%m转换说明符不是C,而是printf的GNU扩展.'%m'转换在errno中打印对应于错误代码的字符串.
我知道格式说明符%是C99中的新内容.它以十六进制形式打印浮点数. …
我们知道C是一种编译语言.根据C语言维基百科,它说:
它被设计为使用相对简单的编译器进行编译,以提供对内存的低级访问,提供有效映射到机器指令的语言结构,并且需要最少的运行时支持.它还说,通过设计,C提供了有效映射到典型机器指令的结构,因此它已经在以前用汇编语言编写的应用程序中得到了持久的使用,包括操作系统,以及用于计算机的各种应用软件.超级计算机到嵌入式系统.
但是当我读到这篇文章时,根据Bruce Eckel的C++ 2中的Thinking,它在第2章中标题为Iostreams :(我省略了一些部分)
最大的障碍是用于变量参数列表函数的运行时解释器.这是在运行时解析格式字符串并从变量参数列表中获取和解释参数的代码.出于四个原因,这是一个问题.
因为解释在运行时发生,所以你无法摆脱性能开销.令人沮丧的是因为编译时格式字符串中存在所有信息,但直到运行时才会对其进行求值.但是,如果您可以在编译时解析格式字符串中的参数,则可以进行硬函数调用,这些函数调用可能比运行时解释器快得多(尽管printf()函数系列通常已经过很好的优化).
这个链接还说:
更类型安全:有了I/O的对象类型,编译器静态地知道.相反,cstdio使用"%"字段动态地计算出类型.
所以在阅读本文之前,我认为解释器不像C那样用于编译语言,但是在C程序执行期间运行时解释器是否也可以使用?读这篇文章之前我错了吗?与Iostream相比,这种运行时解释真的有这么多开销吗?
考虑以下计划:
#include <iostream>
int main()
{
std::cout<<std::ios::showbase<<123<<", "<<std::hex<<123<<", "<<std::oct<<123<<'\n';
}
Run Code Online (Sandbox Code Playgroud)
预期输出:123,0x7b,0173
获得的输出:512123,7b,173(参见现场演示:http://ideone.com/Khzj5j )
但如果我稍微修改上述程序如下:
#include <iostream>
using namespace std;
int main()
{
cout<<showbase<<123<<", "<<hex<<123<<", "<<oct<<123<<'\n';
}
Run Code Online (Sandbox Code Playgroud)
现在我得到了理想的输出.(请参阅http://ideone.com/gcuHbm上的现场演示).
为什么第一个程序输出不正确但第二个程序没有?第一个项目出了什么问题?
我正在阅读有关static_cast运算符的内容.
考虑以下示例:
#include <iostream>
class B { };
class D : public B
{
public:
void fun()
{
std::cout<<"fun() is called\n";
}
};
void f(B* pb,D* pd)
{
D* pd2=static_cast<D*>(pb);
B* pb2=static_cast<B*>(pd);
pd2->fun();
}
int main()
{
B b;
D d;
f(&b,&d);
}
Run Code Online (Sandbox Code Playgroud)
它说:
在下面的示例中,行D*pd2 = static_cast(pb); 是不安全的,因为D可以有不在B中的字段和方法.但是,行B*pb2 = static_cast(pd); 是一个安全的转换,因为D总是包含所有的B.
与dynamic_cast相反,没有对pb的static_cast转换进行运行时检查.pb指向的对象可能不是D类型的对象,在这种情况下使用*pd2可能是灾难性的.例如,调用作为D类成员但不是B类成员的函数可能会导致访问冲突.
我在gcc 4.8.1和MSVS 2010上尝试了它并获得输出fun()被调用.那么这个程序会调用未定义的行为吗?我的程序可以在运行时崩溃吗?C++标准对此有何看法?如果我理解错误,请纠正我.
c++ inheritance static-cast undefined-behavior language-lawyer
请考虑以下程序:(请参阅此处的实时演示).
#include <stdio.h>
struct Test
{
int a;
};
typedef struct Test t;
typedef struct Test t;
int main()
{
t T={9};
printf("%d",T.a);
}
Run Code Online (Sandbox Code Playgroud)
该程序在C11编译器中编译良好,但在C99编译器中编译失败.为什么?是什么原因?我的编译器gcc 4.8.1给出以下警告:
[Warning] redefinition of typedef 't' [-Wpedantic]
[Note] previous declaration of 't' was here
Run Code Online (Sandbox Code Playgroud)