0

In my class: I want to create a public function which has as argument a private member of the class. And be able to call this function from outside of the class.

Something like that:

class MailToTerm
{
    public:
        MailToTerm();
        int getPrivMax(intWithMax priv) {return priv.getMax();} //my public function
        void initPrivMax(intWithMax priv) {return priv.initMax();}
        void getMutexObjPriv(intWithMax priv) {return priv.GetMutexObj();}
        void relMutexObjPriv(intWithMax priv) {return priv.RelMutexObj();}

    private:
        intWithMax cnter_multi_busy ;
        intWithMax cnter_mono_busy ;
        intWithMax cnter_light_busy ;
}

int main(){
    MailToTerm* pt_MailToTerm = new MailToTerm();
    int multi = pt_MailToTerm->getPrivMax(MailToTerm::cnter_multi_busy);
    int mono=   pt_MailToTerm->getPrivMax(MailToTerm::cnter_mono_busy);
}

It doesn't work because cnter_multi_busy is seen as "private", so I'm not allowed to access it. How should I do?

4

5 回答 5

2

我喜欢列举选项,像这样。它们可以很容易地用在 if/else 或 switch 语句中,如果以后添加更多选项,它们也很容易扩展。它还很好地隐藏了实现和私有成员。此外,如果需要,枚举可以用作带有intWithMax:s 的数组中的索引。

class MailToTerm
{
    public:
        enum Options { // or some better name, of course
            CENTER_MULTI_BUSY = 0,
            CENTER_MONO_BUSY
        };
        MailToTerm();
        int getPrivMax(Options o) {
            if (o == CENTER_MULTI_BUSY) return cnter_multi_busy.getMax();
            else if (o == CENTER_MONO_BUSY) return cnter_mono_busy.getMax();
            else // do some error handling
        }

    private:
        intWithMax cnter_multi_busy ;
        intWithMax cnter_mono_busy ;
}

int main() {
    MailToTerm* pt_MailToTerm = new MailToTerm();
    int multi = pt_MailToTerm->getPrivMax(MailToTerm::CENTER_MULTI_BUSY);
}
于 2013-04-23T15:22:48.723 回答
1

因此,您希望有一个对成员变量执行某些操作的函数,但希望在参数中指定哪个函数。我的解决方案是使用数组:

class MailToTerm
{
    public:
        enum MemberIndex {
            CNTER_MULTI_BUSY = 0,
            CNTER_MONO_BUSY = 1
        };

        MailToTerm();
        int getPrivMax(MemberIndex index) {return cnter_busy[(int) index].getMax();}

    private:
        intWithMax cnter_busy[2];
};

然后像这样调用它:

pt_MailToTerm->getPrivMax(MailToTerm::CNTER_MULTI_BUSY);
于 2013-04-23T15:26:37.583 回答
1

您可以将成员公开,也可以创建一个调用原始函数并传递私有成员的公共包装函数。

于 2013-04-23T15:18:07.080 回答
1

我可能会创建一个返回您真正想要的函数的函数,例如:

class MailToTerm
{
    public:
        MailToTerm();
        int getPrivMaxMulti() { return cnter_multi_busy.getMax(); }
        // repeat for mono. 
    private:
        intWithMax cnter_multi_busy ;
        intWithMax cnter_mono_busy ;
}

或者,您当然可以公开这些值,但这可能不是一个好主意。

另一种解决方案是为cnter_ ...私有成员提供访问功能,如下所示:

class MailToTerm
{
    public:
        MailToTerm();

        int getPrivMax(const intWithMax priv) {return priv.getMax();} //my public function

        const intWithMax getMulti() const { return cnter_multi_busy; }
        // ... as above for mono ... 
    private:
        intWithMax cnter_multi_busy ;
        intWithMax cnter_mono_busy ;
}

接着

   int multi = pt_MailToTerm->getPrivMax(pt_MailToTerm->getMulti());
于 2013-04-23T15:23:22.287 回答
0
class MailToTerm
{
    public:
        MailToTerm();
        int getPrivMax() {return cnter_multi_busy.getMax();} //my public function

    private:
        intWithMax cnter_multi_busy ;
        intWithMax cnter_mono_busy ;
}

int main(){
    MailToTerm* pt_MailToTerm = new MailToTerm();
    int multi = pt_MailToTerm->getPrivMax();
}
于 2013-04-23T15:19:17.317 回答