Click here to Skip to main content
15,885,216 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: else does not return expected value Pin
TheGreatAndPowerfulOz26-Jan-11 10:10
TheGreatAndPowerfulOz26-Jan-11 10:10 
QuestionRe: else does nto return expected value Pin
Maximilien26-Jan-11 9:56
Maximilien26-Jan-11 9:56 
AnswerRe: else does nto return expected value Pin
jharn26-Jan-11 10:08
jharn26-Jan-11 10:08 
GeneralRe: else does nto return expected value Pin
TheGreatAndPowerfulOz26-Jan-11 10:14
TheGreatAndPowerfulOz26-Jan-11 10:14 
GeneralRe: else does nto return expected value Pin
Cedric Moonen26-Jan-11 20:35
Cedric Moonen26-Jan-11 20:35 
GeneralRe: else does nto return expected value Pin
Stefan_Lang27-Jan-11 2:20
Stefan_Lang27-Jan-11 2:20 
AnswerRe: else does nto return expected value Pin
Richard MacCutchan26-Jan-11 22:41
mveRichard MacCutchan26-Jan-11 22:41 
QuestionReturning a Reference Pin
Anthony Mushrow26-Jan-11 7:59
professionalAnthony Mushrow26-Jan-11 7:59 
I've come across a situation where I don't understand entirely what's happening regarding references (not pointers).

If we did this for example:
int someNumber = 5;

int& get()
{
  return someNumber;
}

void main()
{
  int a = get(); //a = 5
  get() = 52; //someNumber = 52
  int b = get(); //b = 52, obviously
}

We can change the value of some number by setting the return value of get() (or the long way around int& ref = get(); ref = 52;)

But, in this case I'm not sure what exactly is going on:
class bclass
{
public:
	bclass(){}
	virtual void cheese()=0;
	bclass(const bclass& other){ _asm{int 3} }
};

class dclass : public bclass
{
public:
	dclass(){val=5;}
	void cheese() { _asm{int 3} }
	dclass(const dclass& other){ _asm{int 3} }

	int val;
};

dclass base;

bclass& get()
{
	return base;
}

void main()
{
	dclass argh;
	argh.val = 2;
	get() = argh; //copy constructors not called, no error messages, base.val = 5
	get().cheese();
}


What happens when we try to set the return value of get()? Why doesn't anything happen? I also tried it with just references to bclass so simply trying to set a reference to a bclass to a reference of a different bclass and still nothing.

Also, before anybody says anything I'm not actually trying to set the return value to something else, or set any other variables in some bizarre manner it's just something I've come across quite by accident.

EDIT: Or a much simpler example:
bclass& argh = d2();
bclass& hmm = dclass();
hmm = argh;
hmm.cheese();

Where dclass's implementation of cheese is called
My current favourite phrase: I've seen better!
-SK Genius

Source Indexing and Symbol Servers[^]

AnswerRe: Returning a Reference Pin
dasblinkenlight26-Jan-11 8:26
dasblinkenlight26-Jan-11 8:26 
AnswerRe: Returning a Reference [modified] Pin
TheGreatAndPowerfulOz26-Jan-11 9:39
TheGreatAndPowerfulOz26-Jan-11 9:39 
GeneralRe: Returning a Reference Pin
Emilio Garavaglia26-Jan-11 22:58
Emilio Garavaglia26-Jan-11 22:58 
GeneralRe: Returning a Reference Pin
Stefan_Lang27-Jan-11 1:57
Stefan_Lang27-Jan-11 1:57 
GeneralRe: Returning a Reference Pin
TheGreatAndPowerfulOz27-Jan-11 8:23
TheGreatAndPowerfulOz27-Jan-11 8:23 
GeneralRe: Returning a Reference Pin
Niklas L26-Jan-11 23:20
Niklas L26-Jan-11 23:20 
GeneralRe: Returning a Reference Pin
Stefan_Lang26-Jan-11 23:57
Stefan_Lang26-Jan-11 23:57 
GeneralRe: Returning a Reference Pin
Niklas L27-Jan-11 1:07
Niklas L27-Jan-11 1:07 
GeneralRe: Returning a Reference Pin
Stefan_Lang27-Jan-11 1:20
Stefan_Lang27-Jan-11 1:20 
GeneralRe: Returning a Reference Pin
TheGreatAndPowerfulOz27-Jan-11 8:02
TheGreatAndPowerfulOz27-Jan-11 8:02 
AnswerRe: Returning a Reference [modified] Pin
Stefan_Lang26-Jan-11 23:12
Stefan_Lang26-Jan-11 23:12 
QuestionAny fast and efficient compress/decompress code? Pin
includeh1026-Jan-11 6:53
includeh1026-Jan-11 6:53 
AnswerRe: Any fast and efficient compress/decompress code? Pin
Anthony Mushrow26-Jan-11 7:41
professionalAnthony Mushrow26-Jan-11 7:41 
AnswerRe: Any fast and efficient compress/decompress code? Pin
Rolf Kristensen26-Jan-11 8:45
Rolf Kristensen26-Jan-11 8:45 
QuestionCapturing stdout from an external program Pin
piul26-Jan-11 3:50
piul26-Jan-11 3:50 
AnswerRe: Capturing stdout from an external program Pin
David Crow26-Jan-11 3:55
David Crow26-Jan-11 3:55 
GeneralRe: Capturing stdout from an external program Pin
piul26-Jan-11 4:43
piul26-Jan-11 4:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.