Doubts in Class & function

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()
Last edited on
What is the purpose of class&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.
Thank you for the quick response.
1. I suppose PlatformMgr(void) is used in the context of the class PlatformManager.

2.Can you explain the concept func1().func2() with an example something.
Let say that we have a function that returns a string: std::string func1();

In this example case we don't actually need the text of the string -- only its length.
We could do:
1
2
std::string text = func1();
std::cout << text.size();

However, since only the integer is needed, there is no need to introduce a string variable:
std::cout << func1().size();
PlatformManager & PlatformMgr(void)
That looks like declaration of a function.


I agree - especially as it starts with inline.

Remembering the 'most vexing parse'

If it looks like a function declaration it is a function declaration.
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.
1
2
3
4
5
6
7
8
9
10
#define  CHIP_NO_ERROR 1
 main
{
  ret = PlatformMgr().StartEventLoopTask();
    if (ret != CHIP_NO_ERROR)
    {
        EFR32_LOG("PlatformMgr().StartEventLoopTask() failed");
        appError(ret);
    }
}

in another file
extern PlatformManager & PlatformMgr();. Where PlatformManager is a class.
In another file
1
2
3
4
5
class PlatformManagerImpl
{
private:
  static PlatformManagerImpl sInstance;
}



1
2
3
4
inline PlatformManager & PlatformMgr(void)
{
    return PlatformManagerImpl::sInstance;
}
.
Last edited on
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.  


lines 10/11 and 13/14 do exactly the same thing.
Last edited on
1
2
3
4
inline PlatformManager & PlatformMgr(void)
{
    return PlatformManagerImpl::sInstance;
}


Yes, PlatformMgr is a function taking no arguments and returning a type PlatformManager& (a reference to type PlatformManager).

Usually this would be written as:

1
2
3
4
inline PlatformManager& PlatformMgr()
{
    return PlatformManagerImpl::sInstance;
}

rohithanaydy wrote:
1
2
3
4
5
6
7
8
9
10
class PlatformManagerImpl
{
private:
  static PlatformManagerImpl sInstance;
}

inline PlatformManager & PlatformMgr(void)
{
    return PlatformManagerImpl::sInstance;
}

I have a bad feeling about this.

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

An example: http://www.cplusplus.com/reference/string/string/operator%3E%3E/
istream& operator>> ( istream& os, string& str );
That function returns reference to istream. The istream is a class.
1
2
3
4
5
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
}
Last edited on
@rohithanaydy - does your code compile? What os/compiler are you using?
I have taken this code from GitHub and tried to read using SublimeText
I have one more doubts. Please anyone describe the code to me

1
2
3
4
5
6
7
8
9
class AppTask
{
 private:
 static AppTask sAppTask;
};
inline AppTask & GetAppTask(void)
{
    return AppTask::sAppTask;
}


static AppTask sAppTask; An object can be defined inside the class?
what is the meaning of return AppTask::sAppTask;
That doesn't compile.

L4 sApptask is private to the class AppTask.
L8 sAppTask is tried to be accessed outside of the class. For this to work, L3 would need to public:

Also, sAppTask is declared but not defined. This compiles OK:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class AppTask
{
public:
	static AppTask sAppTask;
};

inline AppTask& GetAppTask(void)
{
	return AppTask::sAppTask;
}

AppTask AppTask::sAppTask;

int main()
{
	const auto a {GetAppTask()};
}

Last edited on
Anyone please describe function return by refernce to object in C++ with example
The example by seeplus did just that.

Here is another:
1
2
3
4
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' );
Last edited on
Topic archived. No new replies allowed.