bryan76 05-05-2002, 09:54 PM Hi, may I know is there any way to call a C++ class function like a C function?
For example:
Class Test {
public:
void Req_Function(void);
};
main(){
Test sample_a;
sample_a.Req_Function();
}
Is there any way to call Req_Function without putting "sample_a." in front? I do not wish to use any C wrapper such as the following:
extern "C" {
void Req_Function(void)
{
sample_a.Req_Function();
}
}
I found that for standard libraries like iostream.h, we can use cout as it is even if it is a class. So anyone can teach me how to do that?
Thank in advance for any advise :)
dolphin 05-06-2002, 12:51 AM hi bryan
As there are some rules to be followed, yet you can do this by making a constructor of the class, because to access something from the class it is necessary to have its object, as it is the console for object oriented programming.
bryan76 05-06-2002, 01:09 AM I'm sorry... I do not really understand what you mean by using a constructor. Can you elaborate? :)
Squirm 05-06-2002, 07:41 AM Originally posted by bryan76
I found that for standard libraries like iostream.h, we can use cout as it is even if it is a class
This is because the << has been defined to handle situations where a cout class is used as the left operand. If you created a class called Hello, you could define the << operator (or any other for that matter) so that you could, in code, have:
void main(void)
{
Hello test;
test << "boo";
}
And the code for defining the class might look something like this:
class Hello {
public:
void operator<<(const char*);
};
void Hello::operator<<(const char* temp)
{
//Do something with temp
//store it, print it, whatever
}
This has nothing to do with functions, though. If you want to define a class function so that you can call it without the class prefix, I dont think that is possible without a wrapper function.
Rezner 05-06-2002, 08:23 AM To elaborate on everything that has been said:
If you want to access a public method in a particular class, you need to use the "dot operator" i.e. class.method() -- alternatively, you can overload an operator in that class (like the "<<" operator in the cout class), like Squirm showed.
However, if you wish to have certain methods and variables set automatically when a variable is declared -- then you need to use a class constructor.
Squirm 05-06-2002, 01:41 PM And to elaborate further, a constructor is a member of the class which is invoked when the class is created. It can take as many parameters as you deem necessary, and can be overloaded just like any other function (infact its the only time when I overload them).
The constructor has the same name as the class in which it resides, and has no return values. It is used to initialize class variables. An example of our Hello class is shown:
class Hello {
public:
Hello(const int); //This is the constructor
void operator<<(const char*);
private:
int junk;
};
//This is the constructor
Hello::Hello(const int number)
{
junk = number;
}
void Hello::operator<<(const char* temp)
{
//Do something with temp
//store it, print it, whatever
}
void main(void)
{
Hello test(4); //The constructor is called here;
test << "boo";
}
Of course, all this will be explained in most good begginer C++ books. :)
bryan76 05-06-2002, 06:13 PM Thx everyone for the replies... I guess I roughly know what to do now... thx alot again :)
Rezner 05-06-2002, 08:06 PM Additionally, it's always a good idea to use a pointer to a class instead. To do so, use the pointer indirection operator ->
class Cool
{
public:
void PrintMessage()
{ cout << "Wow, this is the Cool class method!\n"; };
};
void main() {
// access the class's method
Cool y;
y.PrintMessage();
// below is a more efficient way off invoking methods
Cool *x = new Cool; // create a pointer to the class
(*x).PrintMessage(); // access the class method
x->PrintMessage(); // preferred way (does very same thing)
getchar();
}
bryan76 05-06-2002, 08:52 PM Hi, may i know what are the benefits of using a pointer to a class instead of using the instance itself (meaning using dot operator)?
Rezner 05-06-2002, 09:12 PM A pointer is variable whose value is the address of the desired value. When using single variables, pointers allows the OS to manage memory a tad more efficient. But when using a pointer to an entire object class, the results can be dramatic.
I guess an analogy would be ordering pizza and having them deliver it to your house -- this is a variable. Even though the pizza is delivered to your house, there is an exact replica of it back at the pizza parlor.
Compare that to you driving to the restaraunt and having the pizza waiting on the table for you to eat -- this is a pointer. Once you eat that pizza, it is gone. If other people came, they would eat from the same pizza as you (the original pizza (the variable)). BUT, if somebody ordered a pizza for delivery while you were eating, the restaurant would have to make a new pizza (or maybe just modify a current replica) exactly like the current state of yours and deliver it to them (some missing if need be.)
A pointer to a class works the same way, except that it is a pizza party. You and all of your friends go to the restaraunt and eat the pizza there, instead of having them deliver it. If you don't use a pointer, then the restaruant has to make the exact same pizzas for many, many parties -- and deliver all of them.
In essense, the OS is the pizza parlor.
I donno, I guess that is a lil' abstract. But, I like to conceptualize things like this. Hope it helps.
bryan76 05-07-2002, 01:47 AM thx for the pizza analogy... took me a while to "digest" the pizzas but I finally understood what you mean... thx! :D
|