开发者

Is there a way to call automatically function when class created by new

I need to call function whenever class is created using new. Currently I need to wr开发者_JS百科ite something like this:

MyClass *myClassPointer = new MyClass(...);
myFunction(myClassPointer);

When MyClass is created as local object myFunction cannot be called. MyClass have at lest 2 constructors with different arguments. Is there a way to automatize it, that other programmers didn't need to remember to call it.

I work with quite old framework and myFunction is used for maintenance without breaking it.

edited

myFunction is a public member function of application main window, witch is visible in all code of application. But myFunction my be moved out of class mainWindow and be changed into global function if it is needed.

I was thinking of some kind of macro in header file of myClass, because this way I don't need to inspect all code that is already written, and add those modifications. But I couldn't find any solution.

Thanks in advance for any ideas and suggestions.

After accept.

The problem is because of bad design of MyClass and it's use in framework. I accepted Mark's answer, because MyClass should be created by ClassFactory from the start.


Use a class factory rather than calling new directly. The factory can call myFunction before returning the pointer.


An object factory function is one possible solution, but it makes it more difficult and laborious to define derived classes: while simple, it's very very intrusive.

If you absolutely must do this, then a practical way is to define a macro like this:

#define MYNEW( Type, args ) myFunction( new Type args )

where you let myFunction return its pointer arg. You can use that macro like this:

MyClass* myClassPointer = MYNEW( Type,( blah, blah, blah ) );

That addresses half of the problem, namely how to get rid of the redundancy.

The other half is how to make this safe, to make sure that nobody uses new directly, and only your MYNEW macro. The answer to that is, you can't, but you can make direct use of new so dogdarned convoluted and impractical that nobody will be able to make that mistake inadvertently. For this you simply define an allocation function with extra arguments (such an allocation function is called a "placement new" because the simplest one allows object creation in pre-existing storage, i.e. to "place" an object in memory).

It can go like this:

#include <stddef.h>

// Support machinery:
template< class Type >
Type* myFunction( Type* p ) { return p; }

enum NewCallObfuscation {};
#define MY_NEW( Type, args )    \
    myFunction( new((NewCallObfuscation*)0) Type args )

// Usage:    
struct MyClass
{
    void* operator new( size_t size, NewCallObfuscation* )
    {
        return ::operator new( size );
    }

    MyClass( int, int, int ) {}
};

int main()
{
    //MyClass* p = new MyClass( 1, 2, 3 );      //!Nyet
    MyClass* p = MY_NEW( MyClass,( 1, 2, 3 ) );
}

Having said that, calling a function for every successfully constructed object allocated by new is a pretty specific and pretty unusual requirement. I suspect that the problem that you're trying to solve is much easier. I.e., that you're asking about the wrong thing.

Cheers & hth.,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜