Advertisement

How do I make this work?

Started by July 24, 2000 09:07 PM
5 comments, last by Esap1 24 years, 5 months ago
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
Well i tried all kinds of stuff and i cant make it work. How about making a class template instead.

-Snyper
=============================Where's the 'any' key?=============================
Advertisement
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
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,

hey, how did you do the code box?
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

The Great Milenko"Don't stick a pretzel up your ass, it might get stuck in there.""Computer Programming is findding the right wrench to hammer in the correct screw."
Advertisement
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
[Cyberdrek | ]

This topic is closed to new replies.

Advertisement