如何从类的实例重载成员函数

时间:2022-11-25 08:00:10

I want to make a class where there is a function that is called automatically, to process information stored within this classes instance.

我想创建一个类,其中有一个自动调用的函数,用于处理存储在此类实例中的信息。

However each instance has different values and possibly a different way for that content to be handeled.

但是,每个实例都有不同的值,并且可能有不同的方式来处理该内容。

Therefore I need something simmilar to constructor overloading but in a member function. Where every instance can overload the default function or leave it up to the default to handle the input.

因此,我需要一些类似于构造函数重载的东西,但是在成员函数中。每个实例都可以重载默认函数或将其保留为默认值以处理输入。

How can that be achieved?

怎么能实现呢?

3 个解决方案

#1


0  

Try to Call Functions in Constructor with if else condition like:

尝试使用if else条件调用构造函数中的函数,如:

class abc{
 abc(){
         if username == "member 1"
             functioncall();
         else
             functioncall();
 }

}

#2


0  

As far as I see you need some virtual construction emulation. There is a simple C/C++ way to do it.

据我所知,你需要一些虚拟的构造模拟。有一种简单的C / C ++方法可以做到这一点。

    // Example program
#include <iostream>
#include <string>

struct A; 
typedef void (*cb)(A*);


struct A
{
    int m_a;

    static void foo(A* aref)
    {
       std::cout << "Print a: " <<  aref->m_a << "\n";   
    }


    A(cb b=foo)
    {
        m_a = 100;
        b(this);
    }
};

int main()
{
    A a;

}

It is not very clear, but still does the trick.

目前还不是很清楚,但仍然可以解决问题。

#3


0  

By creating a separate class for the variable behavior Callable describes the local data relating to the function, and by being a class, can be derived.

通过为变量行为创建单独的类,Callable描述了与函数相关的本地数据,并且可以导出为类。

class Callable {
public:
    int m_Value;
    Callable(int value) : m_Value(value)
    {

    }
    void operator()( int val1, double val2 /* whatever makes sense */ ) {

    }
};

Using the function operator void operator()( ...... ) we create a way of making variables of type Callable to look like a function.

使用函数运算符void operator()(......),我们创建了一种使Callable类型的变量看起来像函数的方法。

class Variable {
public:
    Callable myFunction;
    Variable(const Callable & howToCall, /* some more stuff */) :
        myFunction(howToCall)
    { /* stuff */
    }
    void aFunction(int data, double value ) {
        myFunction( data,  value);
    }
};

When calling aFunction the current value of myFunction is called.

调用aFunction时,调用myFunction的当前值。

Finally Variable can change which function it calls, by modifying the value of myFunction....

最后,Variable可以通过修改myFunction的值来改变它调用的函数....

 myFunction = Callable( /* new parameters */ );

#1


0  

Try to Call Functions in Constructor with if else condition like:

尝试使用if else条件调用构造函数中的函数,如:

class abc{
 abc(){
         if username == "member 1"
             functioncall();
         else
             functioncall();
 }

}

#2


0  

As far as I see you need some virtual construction emulation. There is a simple C/C++ way to do it.

据我所知,你需要一些虚拟的构造模拟。有一种简单的C / C ++方法可以做到这一点。

    // Example program
#include <iostream>
#include <string>

struct A; 
typedef void (*cb)(A*);


struct A
{
    int m_a;

    static void foo(A* aref)
    {
       std::cout << "Print a: " <<  aref->m_a << "\n";   
    }


    A(cb b=foo)
    {
        m_a = 100;
        b(this);
    }
};

int main()
{
    A a;

}

It is not very clear, but still does the trick.

目前还不是很清楚,但仍然可以解决问题。

#3


0  

By creating a separate class for the variable behavior Callable describes the local data relating to the function, and by being a class, can be derived.

通过为变量行为创建单独的类,Callable描述了与函数相关的本地数据,并且可以导出为类。

class Callable {
public:
    int m_Value;
    Callable(int value) : m_Value(value)
    {

    }
    void operator()( int val1, double val2 /* whatever makes sense */ ) {

    }
};

Using the function operator void operator()( ...... ) we create a way of making variables of type Callable to look like a function.

使用函数运算符void operator()(......),我们创建了一种使Callable类型的变量看起来像函数的方法。

class Variable {
public:
    Callable myFunction;
    Variable(const Callable & howToCall, /* some more stuff */) :
        myFunction(howToCall)
    { /* stuff */
    }
    void aFunction(int data, double value ) {
        myFunction( data,  value);
    }
};

When calling aFunction the current value of myFunction is called.

调用aFunction时,调用myFunction的当前值。

Finally Variable can change which function it calls, by modifying the value of myFunction....

最后,Variable可以通过修改myFunction的值来改变它调用的函数....

 myFunction = Callable( /* new parameters */ );