Solved

Pointers in C++

Posted on 2004-08-16
3
227 Views
Last Modified: 2010-04-01
Hi

My problem is to do with pointers. What I am trying to do is pass a pointer to a function defined in a seperate file. That function is then going to create an object which the pointer will then reference. Is this at all possible? At the moment, I seem to be getting back nothing from the function, or it is not changing the pointer that I'm passing. Basically it goes like this:


object *obj;

define_pointer(obj);


in the other file:

int define_pointer(object *obj)
{
      obj = create_and_initialise_object(); // this function does return a pointer to the proper object
}

I'm probably doing something stupid, like scoping issues or something, but I don't know what it is.

For some more info: I'm doing this for a DirectDraw project , and the pointer that I'm passing is a LPDIRECTDRAWSURFACE7, and I've got a function which creates the surface and passes a pointer back.

Any help you could provide would be much appreciated.

Thanks
phoenix260182
0
Comment
Question by:phoenix260182
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
>>I'm probably doing something stupid, like scoping issues or something, but I don't know what it is.

No, for the above, you either need a pointer to a pointer or a reference to a pointer, e.g.

int define_pointer(object **obj)
{
     *obj = create_and_initialise_object(); // this function does return a pointer to the proper object
}

or

int define_pointer(object*& obj)
{
     obj = create_and_initialise_object(); // this function does return a pointer to the proper object
}

The above will work.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
BTW, in your originaly code

int define_pointer(object *obj)
{
     obj = create_and_initialise_object(); // this function does return a pointer to the proper object
}

only the 'local' value of 'obj' will change, i.e. point to the proper object. Once the function returns, the variable in the calling code will remain unchanged, since you are passing the pointer 'by value'. My examples above illustrate how to pass 'by reference', so that the contents of the data passed can be changed by the called function.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
FYI:

Most interface are setup so that the pointer is in the return value.
Example:

object *define_pointer(void)
{
    return create_and_initialise_object();
}

object *obj = define_pointer();

This is a cleaner method, and it's more portable with older compilers.
This method also allows you to create and initialize the object at the same time.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now