Binding lvalue to a link

I think I am missing something else in my theoretical premise about this. I know that there were similar records, but I still do not understand.

I have a code like this:

void somefunc1(Word &Key) { somefunc2(Key); } void somefunc2(char &char1) { return; } 
Compiler

generates an error here:

 somefunc2(Key); 

[BCC32 error] Unit1.cpp (830): E2357 Link initialized with "unsigned short" requires lvalue type 'char'

I found out that this is due to ANSI 2003 C ++ rules regarding temporary and references, but I still don't understand what is wrong here.

when i do the c-style conversion:

 somefunc2( *(char*)&Key ) 

fixes the problem.

Can someone hint me what is wrong and why is it wrong?

+4
source share
2 answers

Temporary users cannot be attached to broken links.

You should have written this:

 void somefunc2(const char &char1) { return; } 
+4
source
  WORD &Key; 

A link is always an alias for another object, and it must be initialized with an existing object. Therefore, the above declaration is not valid. Instead, the following is true:

  WORD &Key = alreadyExistingKey; 

[The above is no longer relevant, the question has changed.]

EDIT:

 void somefunc1(Word &Key) { somefunc2(Key); } void somefunc2(char &char1) { return; } 

[BCC32 error] Unit1.cpp (830): E2357 Link initialized with "unsigned short" requires lvalue type 'char'

The compiler tells you that somefunc2 expecting a [link, that is, an alias for] a char . But Key in somefunc1 is instead of Word , which I understand as typedef for unsigned short .

It seems to me that your "c-style" tool brutally reinterprets &Key , which is an unsigned short address, like a char address. So you go to somefunc2 , this is the first Key byte, interpreted as a (signed) char . I assume that the result depends on the essence. I would not rely on this code.

+10
source

All Articles