Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

I need to call function whenever class is created using new. Currently I need to write 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.

like image 216
firescreamer Avatar asked Dec 09 '25 02:12

firescreamer


2 Answers

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

like image 81
Mark Ransom Avatar answered Dec 10 '25 14:12

Mark Ransom


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.,

like image 32
Cheers and hth. - Alf Avatar answered Dec 10 '25 15:12

Cheers and hth. - Alf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!