Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to do it: Pointer to Reference?

Reply
Thread Tools

How to do it: Pointer to Reference?

 
 
JoTo
Guest
Posts: n/a
 
      07-14-2003
Hi Group,

sorry for my for you surely dumb question. But i'm not so familiar with this
reference- and pointerstew in C++ yet.

Let me explain my problem with a little Codesnippet.

If i do the following:

void CServerSock::OnAccept(void)
{
CSocket clientsock;

.... // some irrelevant code here
this->Accept(clientsock);
.... // some irrelevant code here
}

all went fine. The function Accept is a Member of CServerSock and documented
as

void Accept(CSocket &)

I understand it this way, that the Accept-Function takes a Reference to a
CSocket-Object, right? But there is a problem for me. The Documentation for
CSocket says further:

"If the Object goes out of scope, the connection is automatically
closed"

So, as the Object in my a.m. example is created on the stack, it goes out of
scope immediately when the OnAccept()-Function ends. And than the connection
is closed (what i don't want to happen now!)

The documentation for Accept says:

"The referenced Object can be created on the stack or on the heap"

Yeah! That's it! I thought! I have to create my socket-object on the heap
instead. So i tried sth. like that:

void CServerSock::OnAccept(void)
{
CSocket *clientsock = new CSocket();

.... // some irrelevant code here
this->Accept(clientsock);
.... // some irrelevant code here
}

But now the compiler complains with:

"Cannot convert from class CSocket * to class CAsyncSocket &"

(CSocket is derived from CAsyncSocket - so it should work, isn't it?)
So the problem must be, that a csocket* (Pointer) is sth. different from
csocket& (Reference). I read many pages on the net which states that a
Reference is a const pointer to sth. a.s.o. but i never discovered the
general idea and the background.

Tried a lot versions with

- CSocket const *clientsock
- const CSocket *clientsock
- and, and, and, ...

Nothing worked. Sometimes other errors in the compiler occured, often the
same again and again. Sometimes no error occured and i was able to compile.
But then, at runtime, i get a programcrash for the not right reference to
the object.

So my question is:
How can i get (How must i note to get) a Reference to a memory block/Object
that was created on the heap with the new operator. How can i get a & to an
object when i only have a * to it? How can i keep the compiler happy?

I take the syntax alone, but i really will be happy if there are 1 or 2
sentences of explanation why it should be so or so and not the other way. Is
there a simple explanation how i have to imagine a Reference or a Pointer
(the difference) so i am able to understand it and solve my future problems
with such things on my own (i like more to learn than only to solve sth.).

Any good (understandable for such a lamer like me) webpages out there?

Thx. in advance! Your help, hints, urls, explanations, solutions, money
)), best wishes )) are really appreciated!
CU
Joerg Toellner


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-14-2003
On Mon, 14 Jul 2003 10:19:13 +0200, "JoTo" <> wrote:

>void CServerSock::OnAccept(void)
>{
> CSocket *clientsock = new CSocket();
>
> .... // some irrelevant code here
> this->Accept(clientsock);
> .... // some irrelevant code here
>}
>
>But now the compiler complains with:
>
> "Cannot convert from class CSocket * to class CAsyncSocket &"
>


You have a pointer, the argument must be an object, to get an object
(the thing pointed to) from a pointer do


Accept( *clientsock );


But you have a more severe problem, that the CSocket object is never
deallocated.

One possibility could be to make this object a member of CServerSock.


 
Reply With Quote
 
 
 
 
JoTo
Guest
Posts: n/a
 
      07-14-2003
Hello Alf and Group,

"Alf P. Steinbach" <> schrieb im Newsbeitrag
news:...
> >void CServerSock::OnAccept(void)
> >{
> > CSocket *clientsock = new CSocket();
> >
> > .... // some irrelevant code here
> > this->Accept(clientsock);
> > .... // some irrelevant code here
> >}
> >But now the compiler complains with:
> > "Cannot convert from class CSocket * to class CAsyncSocket &"

> You have a pointer, the argument must be an object, to get an object
> (the thing pointed to) from a pointer do
> Accept( *clientsock );


Hmmm! I thought i tried this variant already with the same result (compile
error). But maybe i only think i have done THIS variant in the mess i've
done with "fishing in the dark" . So i try it again and will see.

> But you have a more severe problem, that the CSocket object is never
> deallocated.
> One possibility could be to make this object a member of CServerSock.


Thx for the hint. I have already a member variable

CSocket *myclient[3];

in my Application-Object where i store the pointer in the OnAccept function
(in the irrelevant code sections) with

myapp->myclient[x] = clientsock;

(where x is 0|1|2 in case which clientsock is "free" at the moment)
And of course i'll do a

delete(myapp->myclient[x]);

later (on end of connection, end of app, a.s.o.). Is this enough for what
you suggest?

I need more than one clientsocks (there should be 3 connections at the same
time possible) and use the clientsock later for sending msgs to the
connected participants (thats why i must create it on the heap).

I hope this is what you meant with your "deallocate"-hint. If not please
explain further if possible what i need to do.

But let me first thank you very much for your fast reply. I'll try it and
cross my fingers with hope. ) Sometimes C++ is like black magic and vodoo
for me. But I'll get it ......... sometimes! ))

CU and Thx.
Joerg Toellner


 
Reply With Quote
 
Joerg Toellner
Guest
Posts: n/a
 
      07-14-2003
Thanks Andrew,

i'll try it when i'm at home this evening. I hope the compiler has the same
opinion like you and all the others here. )))

Thx. all for you patience.
Where is the desk where i have to pay the bill now? )

Yours
Joerg Toellner

"Andrew Koenig" <> schrieb im Newsbeitrag
news:...
> Let me summarize the problem.
> You write
> CSocket clientsock;
> this -> Accept(clientsock);
> and it works just fine except for the fact that clientsock is a
> local variable and is therefore destroyed at the wrong time.
> So you want to create the object on the heap instead.
> Here's how to do that:
> CSocket* socket_ptr = new CSocket;
> this -> Accept(*socket_ptr);
> And then, when you're done with the object, delete it:
> delete socket_ptr;



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57