Archived

This topic is now archived and is closed to further replies.

Esap1

How do I make this work?

Recommended Posts

Esap1    122
How can I make this 34????
    
class CTest 
{
	
public:

	int theval;

};



void Function(CTest **input)
{
	input = new CTest*[1];
	input[0]->theval = 34;
}



 
void main()
{
	CTest *Test;
	Test=0;

	Function(&Test);

	printf("other==%d\n",Test[0].theval);
	
}

    
thanks a lot

Share this post


Link to post
Share on other sites
Gorg    248
Like that

            
class CTest {

public:

int theval;
};

void Function(CTest *&input)
{
input = new CTest[1];
input[0].theval = 34;
}

void main(){

CTest *Test;

Test = 0;
Function(Test);
printf("other==%d\n",Test->theval);
}


You are doing a wrong assumption that a ** = *&

try it with some other variable :

CTest **ppTest;

if you do

pptest = &Test;
wich is what happens when you call the function in your case

the only thing that it means is that ppTest CONTAINS the address of Test. Not that it is the address of Test.

This is the small difference between a reference and a pointer :

a pointer points to an object, but a reference IS the object.

So by giving a *& (wich means a reference to a pointer, wich means input will correspond to the same adress than test)it will work because input IS test.

by giving a **(wich means a pointer to a pointer ,wich means input will contain the address of test) it will not work because input POINTS to test. So the value inside input = &Test. So if you do input = new CTest[1], input = new address.

I know it's hard to understand and it is hard to explain, but by playing with some values and checking their addresses and their values in the debugger, you will understand.

One last thing, in your code you do:

input = new CTest*[1];
input[0]->theVal = 34;

this won't work because input = new CTest*[1], just means that your created an array of 1 CTest*. So you still have a pointer but no CTest. You need to do:

input = new CTest*[1];
input[0] = new CTest;
input[0].theval = 34;


but this won't give any value in Test because your are still passing **.

I guess you have enough stuff to give you a headache! Hope you'll figure it out!




Edited by - Gorg on July 24, 2000 11:02:18 PM

Share this post


Link to post
Share on other sites
Esap1    122
Thanks A Lot Gorg. I kinda skipped over that in my programming experience but I avoid it no longer. Thanks a lot, Im pretty sure I REALLY understand it. I also have the function working thanks to you, thanks again,

Share this post


Link to post
Share on other sites
Great Milenko    122
vbisme: looking the FAQ.... theres a link under the reply button at the top of this page


Great Milenko

Words Of Wisdom:
"Never Stick A Pretzel In Your Butt It Might Break Off In There."


http://www.crosswinds.net/~milenko
http://www.crosswinds.net/~pirotech

Share this post


Link to post
Share on other sites
Cyberdrek    100
I just have one little question, why are you using a double pointer to define the "input" variable?

as Gorg was saying: ( going back to my good old dos programming days )

CTest **ppTest;

if you do

*pptest = &Test;

This should work. It is way that I learned to assign something to a double pointer in DOS. Not sure about VC++ though as it does sometimes use weird ways of doing things...







Cyberdrek
Headhunter Soft
DLC Multimedia

Share this post


Link to post
Share on other sites