获取父函数内的子对象的大小

时间:2018-01-25 15:41:23

标签: c++ c++11

我想知道父函数中派生类对象的大小。在我的情况下,父函数是虚拟的,因此我不能使用sizeof(子类名),我必须根据传递的对象动态决定。以下是代码段。我在父类的PrintBalance()中使用了sizeof(* this),但这不起作用。我需要类似的东西。我从重写的子类显式调用父类函数。

class Account
{
protected:
    int var1;
    int var2;
    double m_balance;

    public:

    Account( double d )
    {

        m_balance = d;
    }

    virtual double GetBalance()
    {
        return m_balance;
    }
    virtual void PrintBalance()
    {
        cout<<"size in Parent= "<< sizeof(*this)<<endl;
    }
private:

};

class CurrentAccount : public Account
{
    int var3;
    int var4;
public:
    CurrentAccount(double d) : Account(d) {}

    void PrintBalance()
    {
        Account::PrintBalance();
        cout<<"size in Derived = "<< sizeof(*this)<<endl;
        cout << "Current account balance: " << GetBalance() << endl;
    }
};

int main()
{
    CurrentAccount cAcc(1000);
    CurrentAccount *pCurrent = &cAcc ;
    pCurrent->PrintBalance();
    pCurrent->GetBalance();
}

1 个答案:

答案 0 :(得分:2)

您需要在基类中使用虚方法,并在所有派生类中重写它,返回类/类型的大小。

class Account
{
public:
    //....
    virtual size_t GetSizeOf() const
    {
        return sizeof(*this);
    }
    //....
    virtual void PrintBalance()
    {
        std::cout << "size in Parent= " << this->GetSizeOf() << std::endl;
    }
    //...
};

class CurrentAccount : public Account
{
public:
    //...
    size_t GetSizeOf() const override
    {
        return sizeof(*this);
    }
    //...
    void PrintBalance() override
    {
        Account::PrintBalance();
        std::cout << "size in Derived = " << this->GetSizeOf() << std::endl;
        // ...
    }
};

所以它应该从两种方法打印相同的大小 - http://coliru.stacked-crooked.com/a/37177e4e6e899090

<强>更新

要从基础打印所有派生类的成员,您需要一个允许派生扩展基础的虚方法。

示例1:使用覆盖打印派生类的成员:

class Account
{
protected:
    //....
    virtual void DoPrint() const
    {
        std::cout << "print Account's members" << std::endl;
    }
public:
    //....
    void PrintBalance()
    {
        //std::cout << "size in Parent= " << this->GetSizeOf() << std::endl;
        DoPrint();
    }
};

class CurrentAccount : public Account
{
protected:
    //...
    void DoPrint() const override
    {
        Account::DoPrint();
        std::cout << "print CurrentAccount's members" << std::endl;
    }
};

您还可以更改基础以使用字典甚至打印回调列表。

示例2:打印派生类的成员而不覆盖:(http://coliru.stacked-crooked.com/a/56402cff02a922d2):

#include <functional>
#include <vector>
#include <iostream>

// not using namespace std;

class Account
{
protected:
    int var1 = 1;
    int var2 = 2;
    double m_balance = 3.0;
public:
    std::vector<std::function<void()>> printers;  // Could be made static and pass `this` and as parameter

    Account()
    {
        printers.emplace_back([this]() {
            std::cout << "var1= "<< var1 << "\n"
                      << "var2= "<< var2 << "\n"
                      << "m_balance= "<< m_balance << std::endl;
        });
    }
    //....
    void PrintBalance()
    {
        for ( const auto& p : printers )
        {
            p();   
        }
    }
    //...
};

class CurrentAccount : public Account
{
protected:
    int var3 = 4;
    int var4 = 5;
public:
    CurrentAccount(): Account() {
        printers.emplace_back([this]() {
            std::cout << "var3= "<< var3 << "\n"
                      << "var4= "<< var4 << std::endl;
        });
    }
    //...
};