lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

cast or function pointer or something else?

Fri Aug 15, 2014 10:56 pm

1

Code: Select all

******************************************************************************/
MLCIStream& MLCIStream::operator>>(istream& (*manipulator)(istream&))

void MLCOStream::external_ctor(const MString& description, char* buf, int
                               nBytes)
{
   VERIFY_ISTREAM(*this, manipulator);
}
what's this (stream&(*manipulator)(stream&))?
this (*manipulator) a cast or function pointer? I couldn't tell what it is.



2

Code: Select all

if defined(GNU)
 
  typedef void (*new_handler)();
  extern "C" new_handler set_new_handler (new_handler);
if new_handler is a function pointer, then new_handler set_new_handler (new_handler) means it returns a function pointer and takes in a function pointer as param?

3

Code: Select all

void out_of_memory_handler()
{
 ....}

 set_new_handler(out_of_memory_handler);
shouldn't that be out_memory_hanfler()?

4

Code: Select all

Bool operator==(const MString& str1, const char* str2)
{
   return (strcmp(str1.str, str2) == 0);
}
why one string use & and another use *?

5

Code: Select all

MString::MString(const char* source, int first)
   : refCount(1)
i normally think : is for base class for inheritance, but :refCount is not. so what does : means in this case?

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: cast or function pointer or something else?

Sat Aug 16, 2014 1:29 am

lilzz wrote:1

Code: Select all

******************************************************************************/
MLCIStream& MLCIStream::operator>>(istream& (*manipulator)(istream&))

void MLCOStream::external_ctor(const MString& description, char* buf, int
                               nBytes)
{
   VERIFY_ISTREAM(*this, manipulator);
}
what's this (stream&(*manipulator)(stream&))?
this (*manipulator) a cast or function pointer? I couldn't tell what it is.
manipulator is a pointer to function that takes one argument (a reference to a stream object) and returns a reference to a stream object. As the name suggests it is a stream manipulator.
lilzz wrote: 2

Code: Select all

if defined(GNU)
 
  typedef void (*new_handler)();
  extern "C" new_handler set_new_handler (new_handler);
if new_handler is a function pointer, then new_handler set_new_handler (new_handler) means it returns a function pointer and takes in a function pointer as param?
Yes, it is code to change the new handler. It sets the new handler using the function pointer passed in and returns the previous new handler, so that you can go back to the previous behaviour.
lilzz wrote: 3

Code: Select all

void out_of_memory_handler()
{
 ....}

 set_new_handler(out_of_memory_handler);
shouldn't that be out_memory_hanfler()?
The name suggests it handles the situations when the program runs out of memory. I am not sure what you mean by hanfler
lilzz wrote: 4

Code: Select all

Bool operator==(const MString& str1, const char* str2)
{
   return (strcmp(str1.str, str2) == 0);
}
why one string use & and another use *?
MString is a class that represents a string, while const char* is a C style string. A C style string is simply a a char* (pointer), while MString is a class so it is passed by reference to avoid making a copy.
lilzz wrote: 5

Code: Select all

MString::MString(const char* source, int first)
   : refCount(1)
i normally think : is for base class for inheritance, but :refCount is not. so what does : means in this case?
This is a member initialization list.

Return to “C/C++”