I am new to C++, only know some simple c++ basics. While studying some codes in c++ related to class, I have some doubts on the followings
1.inline PlatformManager & PlatformMgr(void). Here PlatformManager is a class. What is the purpose of class&function(I am not asking what is class and what is function).For what a class is anding with a function.
2. GetAppTask().StartAppTask(). What is the purpose of a function().function()
member function can access member variables. This is one concept of object oriented programming. Basically: The class is the context and only for this context you have functions.
What is the purpose of a function().function()
When a function returns an object you can directly access the functions of this object.
PlatformManager & PlatformMgr(void)
That looks like declaration of a function.
The name of the function is PlatformMgr
The function does not take any parameters. One does not need the void in C++ to show that.
One could write: PlatformManager & PlatformMgr()
The function returns PlatformManager &. A reference to object that has type PlatformManager
The & is used for three different purposes:
* Unary operator that gives address of an object
* To declare a reference
* Binary operator that computes bitwise AND
It does depend on context, which role a & has.
1. I suppose PlatformMgr(void) is used in the context of the class PlatformManager.
That may or may not be correct. The PlatformMgr() function could be a stand-alone function (not a member function of a class) that simply returns a reference to an object of type PlatformManager. In this case, it is not "used in the context of the class PlatformManager."
I guess it's possible that the function is being declared within the declaration of the PlatformManager class. In this case, the keyword inline would be unnecessary. In this case it would be "used in the context of the class PlatformManager."
It all depends on the context surrounding this code snippet. You haven't even given us an entire declaration or definition, so it's really hard to tell.
this code you have may be too complicated for the most basic concepts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class simple
{
string s;
public:
string& get_s() {return s}; //get_s returns a reference to s.
//That grants full access to s for the caller, so you may as well have made s public here.
};
...
simple x;
cout << x.get_s().length(); //get_s returns a string which is an object that has its own
//functions... at first it can help you to see this by doing it explicitly:
string tmp = x.get_s();
cout << tmp.length(); //tmp isn't necessary, but by using it, you get back to the familiar syntax.
* Standalone function PlatformMgr() returns PlatformManagerImpl::sInstance
* The PlatformManagerImpl::sInstance is a private static member of class PlatformManagerImpl and has type PlatformManagerImpl
* However, PlatformMgr() returns reference to PlatformManager, not to PlatformManagerImpl
* Furthermore, standalone non-friend functions do not have access to private members of classes
The ret = PlatformMgr().StartEventLoopTask(); is like one would have written: ret = PlatformManagerImpl::sInstance.StartEventLoopTask();
rohithanaydy wrote:
Where PlatformManager is a class.
Classes are not special; a typename is a typename. Functions can return class objects just like primitive objects.
string word;
if ( cin >> word ) { // This is a call of operator>>( cin, word ), which returns reference to cin
// How is cin used as condition? http://www.cplusplus.com/reference/ios/ios/operator_bool/
cout << "Input was successful\n";
}
1 2 3 4 5 6 7 8
int func1() { // returns int
return 42;
}
std::vector<double> func2() { // returns std::vector<double>, a class
std::vector<double> v( 42, 3.14 );
return v; // vector that has 42 elements, each with value 3.14
}
std::vector<int> odds( 10 );
for ( int e=0; e < odds.size(); ++e ) {
odds[e] = 1+2*e;
}
The odds[e] is a function call that returns reference to element e of vector odds.
We can modify the element via the reference.
Here is a third example: cout << "Answer is " << 42 << '\n';
The cout << "Answer is " is a function call tha returns a reference to cout.
That is why the oneliner is a serie of three function calls:
1 2 3
cout << "Answer is "
cout << 42
cout << '\n'
Or, in non-operator syntax: operator<<( cout, "Answer is " ).operator<<( 42 ).operator<<( '\n' );