?
Solved

Pointers in C++

Posted on 2004-08-16
3
Medium Priority
?
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 11812006
>>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
ID: 11812028
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
ID: 11812443
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

770 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