-2

I am working on this C++ project that uses an open source library that is not well documented, and I came across this code block:

IDataObserver* GetDataObserver()
    {
        return &mFDO;
    }

and also this object creation:

ICommandAcceptor* mpCommandAcceptor;

I found out that IDataObserver is a class in this library. I have read of a pointer to a class and a pointer to a memory location and values. But what would I call this "IDataObserver* GetDataObserver()" ? Is it even a pointer? Does this mean that GetDataObserver() is an instance of that class or what? On the object creation, mpCommandAcceptor object was created from a class called "ICommandAcceptor" but what's up with the * sign? Thanks.

cidi30bg
  • 39
  • 8
  • You need to provide additional context. `GetDataObserver()` is a function. It can be global function, it can be function in a `namespace`, it can be a member function of a class. – R Sahu Jan 04 '15 at 07:03
  • `GetDataObserver()` is a function that returns a pointer to an object of type `IDataObserver`. There is nothing unique or interesting about this code. This is how functions are declared and implemented. – Brent Bradburn Jan 04 '15 at 07:08
  • There's no such thing as a pointer to a class in C++. Pointers point to objects or functions. – Mat Jan 04 '15 at 07:10
  • You "have read of a pointer", but you don't know what the * sign means? What exactly have you read about pointers? – jogojapan Jan 04 '15 at 07:42
  • @jogojapan, I have read that if i have a variable A=1, I can creat a pointer called say p to point to the address in memory where the value "1" is stored using p=&A, and i can also get the value using p=*A. But in this question, I have an asterisk between a class and a function. that i don't understand – cidi30bg Jan 04 '15 at 07:57
  • You really need to go and learn some basic C++. There's a [list of good books here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – juanchopanza Jan 04 '15 at 08:01
  • @obiigbe91 Could you read again the text where you read that? You need to look really carefully at how the asterisk is placed. It is used in two ways: To declare pointers, and to dereference them. In the declaration, the asterisk is placed after the type name: `int *p`, but when dereferencing (to get the value), there is no type name: `*p`. These two usages are fundamental, and you need to remember them very carefully. Stackoverflow won't help more with this than simply reading a standard text book. – jogojapan Jan 04 '15 at 08:18

3 Answers3

0

I assume that by asking what is GetDataObserver() , you mean what it will return .

The function is of return type IDataObserver* , I.e pointer to an object of class IDataObserver.

Mat
  • 202,337
  • 40
  • 393
  • 406
Abhishek Gupta
  • 1,297
  • 13
  • 28
0
ICommandAcceptor* mpCommandAcceptor;

Here, mpCommandAcceptor is a pointer to a variable of type ICommandAcceptor.
Consider int* i which implies i is a pointer to a variable of type int.
In the above case, its a pointer to an instance of the type class ICommandAcceptor.

GetDataObserver() function does not return a pointer to a class; it returns a pointer to an instance of the type class IDataObserver, an object of the class.
In this case, the API function is returning an instance of the class.

IDataObserver mFDO ; // uses the constructor that instantiates the class.
IDataObserver* GetDataObserver(){
    return &mFDO;
}
int main() {
    IDataObserver* test = GetDataObserver(); //Calls the global function to get an instance of the data observer.
    return 0;
}
thepace
  • 2,221
  • 1
  • 13
  • 21
  • What is the point you are trying to make with this example? Are you trying to explain how the `getInstance()` method could be used? Or are you trying to describe alternative ways of implementing the function? And if so, why do you explain that? – jogojapan Jan 04 '15 at 07:48
  • The question was if its a member function. I wished to clarify that it can be both, a member function or not, as mentioned above. – thepace Jan 04 '15 at 08:05
  • Why don't you create an example of that then? You could define an example with `GetDataObserver()` as a member function, and another example where it is a global funcation. Instead you seem to assume that the function could be a member *by being the constructor* of the class, and you claim that constructors "return" objects (which isn't really what constructors do by the way). – jogojapan Jan 04 '15 at 08:14
  • I got your concern. Will do that. – thepace Jan 04 '15 at 08:44
0

GetDataObserver() is just an function which is used to return the object-pointer of object mFDO. mpCommandAcceptor is an object-pointer value of ICommandAcceptor.

leonsephi
  • 1
  • 1