Click here to Skip to main content
15,610,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is this style OK?

#include "BaseClass.h"

CMyClass::AFunction( CData & goodData )

CBaseClass * baseClass;               // don't really need an instance      
baseClass->AgentFunction( goodData ); // just the use of its function


I like the simplicity. I noticed I couldn't do it by embedding CBaseClass or using CBaseClass::

Also, I noticed I didn't have to initialize to NULL, and wondered should I say = 0, or = NULL.
ARopo 20-Oct-10 8:35am    
Am I going mad, if AgentFuntion is not static it will crash as soon as it accesses 'this' none of the other answers seem to agree, what is more a release build in VC6 for certain will contain a random address in baseClass. If the function is static then it would look much clearer like the CBaseClass::AgentFunction
[no name] 20-Oct-10 9:39am    
@ARopo: As I said in my reply, all is well as long as the function doesn't try to access a member variable.
ARopo 21-Oct-10 4:10am    
If it doesn't access a member variable then it should be static. Otherwise it will make maintence difficult e.g. some one in the future may change the function to access a member variable but not notice all the places where it is called without an instance
Aescleal 21-Oct-10 8:23am    
I don't think any of us would advocate doing what Brian's proposed. Well unless you're using a compiler that doesn't support C++ properly in which case all bets are off.

Brian Bennett wrote:
CBaseClass * baseClass; // don't really need an instance
baseClass->AgentFunction( goodData ); // just the use of its function

If you like simplicity why then didn't you make AgentFunction method static?

Share this answer
Brian Bennett 20-Oct-10 7:44am    
Because most of the data and functions inside of it aren't static.
CPallini 20-Oct-10 7:50am    
That does not prevent you making (that particular method) static.
Brian Bennett 20-Oct-10 8:25am    
You must make sure that a function invoked this way doesn't make use of any member variables of that class (all through its call chain).
Share this answer
Cpallini's answer covers the simulation of a static member function.

If you're using a C++98 compiler then use 0 rather than NULL. 0 is type compatible with any pointer type in C++ and if the OS wants something else it's up to the compiler to do the conversion. NULL is only there as a comfort blanket to C programmers and is (on every C++ compiler I've used, 10 of them) defined as 0.

If you're using C++0x then you can use nullptr. This is a variable of type nullptr_t which, again, is type compatible with any other pointer type.



PS: In response to the comment below...

The problem with NULL is that it gives a fig leaf over the embarrassment of 0 for the "null" pointer value in C++. People used to C think that NULL is (void *)0, however in C++ this is complete rubbish. They then get very confused when they try and instantiate a template or call an overloaded function with it and something completely screwy happens. On the other hand if you use plain old 0 then you'll be under no illusions that it's ambiguous and won't be (so) surpised when the int specialisation or override gets created or called.
Share this answer
ARopo 20-Oct-10 8:42am    
I agree that in practice NULL is nerely always 0 and nullptr is better if available. I don't like the refering to a pointer as 0 because 0 is a value where as the notion of NULL is no value more appt for a pointer. My main point was always initialise a pointer so you know what value it starts with otherwise you can't predict what value it will start with at runtime.
ALWAYS inialise a pointer to NULL or a valid address. in the above example a debug build will probably set baseClass to 0 cause a null pointer exception, a release build will set it to a random addess which almost certainly will be the wrong address causing an access violation.

your example has no object instance! if AgentFunction is static call like this: CBaseClass::AgentFunction( goodData )

Also baseClass is a bad name for an object instance, more like baseObj

One more thing use NULL or nullptr not 0, because NULL may not be euivalent 0 it could be a pointer to 0. This is os dependant.

Assuming CBaseClass is not abstact your example could work like this
CBaseClass().AgentFunction( goodData ); 
Share this answer
Why not simply call it as

Share this answer
Brian Bennett 23-Oct-10 7:55am    
Because it was an abstract base class, the compiler wouldn't let me:)
Emilio Garavaglia 23-Oct-10 13:05pm    
I mean, to place that call in the body of your derived class.
The fact the base is abstract doesn't matter, if CBaseClass::AgentFunction exist!. But ... does it exist?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900