小编Cha*_*cui的帖子

关于堆栈上的动态数组的C++

据我所知,C++/C不支持堆栈上的动态数组.在以下说明中:

int data[n] ; // if the n is not decided at compiling time ,this leads to error
Run Code Online (Sandbox Code Playgroud)

但最近,我读了一些其他人的代码如下:

//**
It seems the n number can not be decided at compling time,but when I run it , if i fprintf the formation, each time i got the correct array size !!!!!!
the G++ version is 4.7.1 
Is this because the G++ 4.7.1 support C++11 x which allow dynamic array?
 **//

#include <cstdio>
#include <algorithm>
using namespace std;

#include <stdio.h>

char …
Run Code Online (Sandbox Code Playgroud)

c++ arrays dynamic

3
推荐指数
1
解决办法
2019
查看次数

有没有办法获得C++虚拟成员函数地址

我搜索了这篇文章: C++:使用成员函数指针获取函数虚拟'地址'

为了测试虚拟成员函数是否通常位于对象的起始地址,我编写了如下代码:

#include <pwd.h>
#include <string.h>
#include <stdio.h>

class Base
{
public:
    int mBase1;
    char mBase2;
    virtual void foo()
    {
        fprintf(stderr,"Base foo called\n");
    }
};

class Child: public Base
{
public:
    int cc;
    virtual void foo()
    {
        fprintf(stderr,"Child foo called");
    }
};


int main(int argc, char *argv[])
{
    Base bb;
    fprintf(stderr,"[mBase1 %p][mBase2 %p] [foo %p]\n",&bb.mBase1,&bb.mBase2,&Base::foo);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在编译时,我收到了一个警告:

test.cpp:30:88: warning ‘%p’ expects argument of type ‘void*’, but argument 5 has type ‘void (Base::*)()’ [-Wformat]
Run Code Online (Sandbox Code Playgroud)

输出是:

[mBase1 0xbfc2ca38][mBase2 0xbfc2ca3c] …
Run Code Online (Sandbox Code Playgroud)

c++ virtual function

2
推荐指数
1
解决办法
2574
查看次数

标签 统计

c++ ×2

arrays ×1

dynamic ×1

function ×1

virtual ×1