简体   繁体   English

仅当派生类知道其大小时如何在基类函数中使用成员变量

[英]How to use a member variable in a base class function when its size is only known to the derived class

I want to implement a function in a baseclass that uses members of its derived classes. 我想在使用其派生类成员的基类中实现一个功能。 So with each DerivedClass, I would have a member of a different value. 因此,对于每个DerivedClass,我将拥有一个具有不同值的成员。 Here's an example. 这是一个例子。 The BaseClass has member function Foo() which uses variable arrStr . BaseClass具有成员函数Foo() ,该函数使用变量arrStr This content of this NUL terminated char array however is only to be found in a derived class. 但是,此NUL终止的char数组的此内容只能在派生类中找到。 How can I make BaseClass "know" the variable arrStr without knowing its size? 如何在不知道变量arrStr情况下使BaseClass “知道”变量arrStr Is that even possible? 那有可能吗?

    class BaseClass
    {
    public:
        BaseClass();
        ~BaseClass();

    protected:

    void Foo()
    {
     prtinf("%s\n", arrStr);
    };

    };

    class DerivedClass : public BaseClass
        {
        public:
            DerivedClass();
            ~DerivedClass();

            protected:

            char arrStr[] = "FooString!";
     };

Add (pure) virtual access functions in your base class which you implement in the derived class. 在派生类中实现的基类中添加(纯)虚拟访问函数。

#include <iostream>
#include <string>

class BaseClass
{
protected:
    void Foo()
    {
        std::cout << GetString() << std::endl;
    }

private:    
    virtual std::string GetString() const = 0;
};

class DerivedClass : public BaseClass
{
private:
    virtual std::string GetString() const override
    {
        return "FooString!";
    }
};

Live demo 现场演示

How can I make BaseClass "know" the variable arrStr without knowing its size? 如何在不知道变量大小的情况下使BaseClass“知道”变量arrStr? Is that even possible? 那有可能吗?

You insert it as a parameter when calling Foo: 您在调用Foo时将其作为参数插入:

class BaseClass
{
public:
    virtual void Process() = 0;
protected:
    void Foo(char* str) // <-- value inserted here as parameter
    {
        prtinf("%s\n", arrStr);
    }
};

class DerivedClass : public BaseClass
{
public:
    void Process() override
    {
        Foo(arrStr); // call in derived class, with derived value
    }
protected:
    char arrStr[] = "FooString!";
};

Client code: 客户代码:

BaseClass * b = new DerivedClass;
b->Process(); // call Foo(arrStr) internally

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当派生类的数据成员从其基类的数据成员继承时,如何防止不需要的行为? - How to prevent unwanted behavior when a data member of a derived class inherits from a data member of its base class? 当我使用根据基类定义的成员函数指针时,编译器如何调用派生成员函数? - How does the compiler call a Derived member function, when I use a member function pointer defined in terms of the Base class? 如何使用基础 class ZC1C425268E68385D1AB5074C17A9 访问派生的 class 成员 function? - How to access derived class member function using Base class function? 指向派生 class 中的基 class 作为成员变量的指针 - Pointer to base class in derived class as a member variable 如果基类具有在成员函数中修改的私有数据成员,派生类如何使用该函数? - If a base class has private data members that are modified in a member function, how does a derived class use that function? 基类函数访问派生类成员 - Base class function accessing derived class member 派生类不调用基类的成员函数 - Derived class not calling member function of base class 如何注册派生的 class 成员 function 指针与基数 class - How to register a derived class member function pointer with a base class 派生类如何使用基类的受保护成员? - How can a derived class use a protected member of the base class? 如何使用基类指针引用派生类成员? - how to use a base class pointer refer to a derived class member?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM