Solved

Pointers in C++

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

738 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