Solved

Pointers in C++

Posted on 2004-08-16
3
228 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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…
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 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.

786 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