Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How is this possible?

Status
Not open for further replies.

iceice

Programmer
Feb 9, 2006
6
GB
Coming from other programming languages I have ssen something unusual in C++

I was looking at a some code where someFunction() was defined globally

The programmer then went on to instantiate an object as follows

sav=(SObj*)GetWindowLong(hwnd,GWL_USERDATA);

He then made the following call

sav->someFunction();

How is this possible since someFunction is not a member of SObj

 
Is SObj derived from another class that someFunction() is a member of? That's what I'd guess. Any class that is derived from another class has access to the parent/super class methods and properties, depending on access allowed in the class declaration.

class SObj : public MObj
{
//SObj class member declarations here
}

would give SObj objects access to all public members of the MObj class, and all public data members of any class that MObj is derived from.

Lee
 
When you speak from global function, then your program is not pure OOP program. In OOP every function was writen in a class, it can be member function or friend function or static function, but it is meaningless to define a global function. Usually in C you will write many subfunctions. But in OOP for example in C++ or Java. No one speak from global function.

When you want it be a global function, my suggestion for you, write this function as a public static function in a approciate class
 
kai2005 said:
But in OOP for example in C++ or Java. No one speak from global function.
How do you get the program to start then since main() has to be a global function in C++?
 
right, main() is a global function, because you need a stage for every player.
But when you think in OOP programming, the important thing is to create a Model, which simulated the problem, which you want solve, and this Model is made up of the different Objects.
When you understand what this means, then you should try to design the classes for the Objects, which is a component in the Model.
So you see, every function in OOP is Method of some class, of course it can be static function, like as global function. But no function in OOP should be alone being.
And the main() is just a stage, in which the objects(players) do their work(play), so that the Model(your problem) would be alive(solved).
 
I'd say that you should use objects as much as possible, but there are some cases where using objects instead of functions is impractical or even impossible (at least in C++).
 
C++ simply doesn't support 100% pure OOP.

If that is a problem, use another language, like SmallTalk which is very OO.

Being 100% OO doesn't necessarily make sense in a non-academic world though...



/Per
[sub]
www.perfnurt.se[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top