小编jll*_*dom的帖子

使用new运算符将对象复制到堆而不知道其类型

我有一个疑问,下面的函数可以接收类型A的对象或派生类型的东西.

A *copyToHeap(A &obj) {
    A *ptr=new A(obj);
    return ptr;
}
Run Code Online (Sandbox Code Playgroud)

如果我们这样称呼它:

//B inherits from A
B bObj;
B *hPtr=copyToHeap(bObj);
Run Code Online (Sandbox Code Playgroud)

指向的对象hPtr实际上是A型还是B型?这样做安全吗?

c++ inheritance new-operator

9
推荐指数
2
解决办法
4136
查看次数

由statvfs()为文件系统计算的已用空间大于fs中所有文件的大小总和

我有一个50MiB的小分区,格式为ext4,只有一个包含一组照片的目录,安装在/ mnt/tmp上.

然后我statvfs()用来计算分区中的已用字节,并lstat()用于计算里面每个文件的大小,为此我编写了这个程序:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/statvfs.h>
#include <stdint.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>

//The amount of bytes of all files found
uint64_t totalFilesSize=0;

//Size for a sector in the fs
unsigned int sectorSize=0;

void readDir(char *path) {
    DIR *directory;
    struct dirent *d_file;  // a file in *directory

    directory = opendir (path);

    while ((d_file = readdir (directory)) != 0)
    {
        struct stat filestat;
        char *abPath=malloc(1024);
        memset(abPath, 0, 1024);
        strcpy(abPath, …
Run Code Online (Sandbox Code Playgroud)

c linux filesystems

5
推荐指数
1
解决办法
7760
查看次数

标签 统计

c ×1

c++ ×1

filesystems ×1

inheritance ×1

linux ×1

new-operator ×1