Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Pointers in C++

Posted on 2004-08-16
3
Medium Priority
?
233 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

610 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