473,796 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Reference of Pointers

Hello all,
I'm currently working on a 3D graphic engine but I'm stuck with a
small problem on references of pointers.
Some of my c++ class are complex and use many pointers so I decided to
force initialisation of objects through their static function Create,
which has a parameter IN-OUT that will contain the instance of the
created object.

Here is a sample that speaks better than words :

--------------------------------------------------------------------------
typedef class Texture* PTEXTURE;
// With this, PTEXTURE& is a reference of a pointer to Texture

class Texture:
{
protected:
Texture( void ){} // Constructor, do nothing, use Create

public:
static void Create( PTEXTURE& ); // PTEXTURE& is a reference of a
pointer to the texture to create

// Many other stuff that is not important here

};

// Implementation of the Create Function
void Texture::Create ( PTEXTURE& pTexToCreate )
{
pTexToCreate = new Texture; // memory alloc

// Some init...

if( badInit )
{
delete pTexToCreate;
pTexToCreate = NULL;
}
}

// A small example of how I use this
void main( void )
{
PTEXTURE pMyTexture;
Texture::Create ( pMyTexture );
if( pMyTexture == NULL )
// Error creating the texture
}

--------------------------------------------------------------------------

Hope this code is clear enough ;)
Then there is my problem : pMyTexture will never be NULL!! even if
there IS a problem in the Create function and the line "pTexToCrea te =
NULL" is executed by the program. Using the debugger shows that
pTexToCreate is NULL, however when going out of the function,
pMyTexture has a random value pointing to nothing with sense.
I really don't understand that, because using references should affect
the original value passed as a parameter (which is true because when
initialization is successfull, pMyTexture is pointing to a valid
Texture). And I don't want to use pointers of pointers to solve this
problem.

Can anyone help please ? I just would like pMyTexture to be NULL when
Create fails :o/

Vianney
Jul 23 '05 #1
2 1453
V. de Bellabre wrote:
I'm currently working on a 3D graphic engine but I'm stuck with a
small problem on references of pointers.
Some of my c++ class are complex and use many pointers so I decided to
force initialisation of objects through their static function Create,
which has a parameter IN-OUT that will contain the instance of the
created object.

Here is a sample that speaks better than words :

--------------------------------------------------------------------------
typedef class Texture* PTEXTURE;
// With this, PTEXTURE& is a reference of a pointer to Texture

class Texture:
What's the colon doing there?
{
protected:
Texture( void ){} // Constructor, do nothing, use Create

public:
static void Create( PTEXTURE& ); // PTEXTURE& is a reference of a
pointer to the texture to create

// Many other stuff that is not important here

};

// Implementation of the Create Function
void Texture::Create ( PTEXTURE& pTexToCreate )
{
pTexToCreate = new Texture; // memory alloc

// Some init...

if( badInit )
{
delete pTexToCreate;
pTexToCreate = NULL;
}
}

// A small example of how I use this
void main( void ) ^^^^^^^^^^^^^^^ ^^^
Change this to

int main()
{
PTEXTURE pMyTexture; ^^^
Initialise the pointer here, to 0 if there is no better value.
Texture::Create ( pMyTexture );
if( pMyTexture == NULL )
// Error creating the texture
}

--------------------------------------------------------------------------

Hope this code is clear enough ;)
Then there is my problem : pMyTexture will never be NULL!! even if
there IS a problem in the Create function and the line "pTexToCrea te =
NULL" is executed by the program. Using the debugger shows that
pTexToCreate is NULL
Where? Inside the function?
, however when going out of the function,
pMyTexture has a random value pointing to nothing with sense.
Seems that your stack gets blown to pieces.
I really don't understand that, because using references should affect
the original value passed as a parameter (which is true because when
initialization is successfull, pMyTexture is pointing to a valid
Texture). And I don't want to use pointers of pointers to solve this
problem.

Can anyone help please ? I just would like pMyTexture to be NULL when
Create fails :o/


Something else can be a problem. If I take the code you posted, get it
to compile (by fixing it up a bit), it behaves as expected. So, the
cause of your trouble is not in the code you showed.

Try to find uninitialised pointers or memory overruns.

V
Jul 23 '05 #2
V. de Bellabre wrote:
Some of my c++ class are complex and use many pointers so I decided to force initialisation of objects through their static function Create,
which has a parameter IN-OUT that will contain the instance of the
created object.
An alternative would be to simply have your Create function return
a pointer. OUT parameters have their place, but all else being equal,
simply returning a value is usually more clear.
typedef class Texture* PTEXTURE;
// With this, PTEXTURE& is a reference of a pointer to Texture
Personally, I think hiding a pointer behind a typedef like this
obfuscates more than it clarifies. Yes, I know the Windows header
files do this all the time, but I suspect that's a holdover from
the bad old 16-bit days of __near and __far pointers.
class Texture:
{
protected:
Texture( void ){} // Constructor, do nothing, use Create
Is Texture meant to be derived from? If so, you should probably
add a virtual destructor. If not, the ctor should be private
instaed of protected.
public:
static void Create( PTEXTURE& ); // PTEXTURE& is a reference of a
pointer to the texture to create
Or:

static Texture* Create();

Problem solved, and no need for reference-to-pointer.
// Implementation of the Create Function
void Texture::Create ( PTEXTURE& pTexToCreate )
{
pTexToCreate = new Texture; // memory alloc

// Some init...

if( badInit )
{
delete pTexToCreate;
pTexToCreate = NULL;
}
}
The code above should work as you intended. The only thing I
can think of is there's undefined behavior somewhere else in
code you snipped. You might try taking the stripped-down code
you posted and edit it to be actually compilable, then run it
to see if the problem still manifests.

As a matter of style, I'd prefer to see Create return a
pointer. There's no need for an OUT or IN/OUT parameter here.

Better yet, why not do the initialization in the ctor as C++
is meant to be used? If initialization failed, the ctor would
throw an exception. (My guess is you're using a factory method
instead because you want to return NULL instead of throwing an
exception on failure.)
// A small example of how I use this
void main( void )
{
PTEXTURE pMyTexture;
Texture::Create ( pMyTexture );
if( pMyTexture == NULL )
// Error creating the texture
}


The return value of main must be int. The above is not legal
C++, although some compilers accept it.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4959
by: cppaddict | last post by:
I understand that there are a couple differences between reference variables and pointer: 1) reference variables *must* be initialized. 2) You cannot change what a reference variable refers to. My question is: In what situations is it better to use a reference variable over a pointer? Also, are there other differences besides the ones I listed above?
7
1889
by: _ed_ | last post by:
I'd like to build a class or struct composed of pointers to variables. Does this require dropping into an 'unsafe' block, or is there a trick? .... int value1 = 1234; bool value2 = false; string value3 = "Hello Detroit"; public class x {
5
2930
by: removeps-generic | last post by:
Hi. I'm using placement new to re-initialize part of an object. Is this OK? struct BaseImp { All& r_all; BaseImp(All& all) : r_all(all) }; struct Calc::Imp : public BaseImp
14
7190
by: Vols | last post by:
If the people ask what is the different between pointer and reference, what is the brief and good answer? I say " pointer could point to NULL, but there is no null reference", What is your opinion? Vol
29
3656
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a string to a integer number. bool Convert(const string& str, int* pData); bool Convert(const string& str, int& data);
15
2684
by: arnuld | last post by:
-------- PROGRAMME ----------- /* Stroustrup, 5.6 Structures STATEMENT: this programmes *tries* to do do this in 3 parts: 1.) it creates a "struct", named "jd", of type "address". 2. it then adds values to "jd" 3.) in the end it prints values of "jd".
41
3689
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what pointer and reference are and how they relate to each other.
68
4660
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a temporary but it was stated that it would not. This has come up in an irc channel but I can not find the original thread, nor can I get any code to work. Foo& Bar( int Val ) { return Foo( Val ); }
275
12418
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10467
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10244
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4130
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.